Identity DB context / hint no plural tables

asp.net-mvc asp.net-mvc-5 c# entity-framework entity-framework-6

Question

I already have a working DB context with the following EF setup.

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Configure Code First to ignore PluralizingTableName convention 
            // If you keep this convention then the generated tables will have pluralized names. 
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();
}

To extend the Identity model supplied by MVC to connect to a person in my pre-existing DB Context, I adhered to a MSDN content.

App User

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
    // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
    // Add custom user claims here
    return userIdentity;
}

public virtual Person LinkedPerson { get; set; }//My reference to existing person definition

}

Context

  public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public DbSet<Person> People { get; set; }

    }

As long as I don't use the register or login features, the software functions. However, after I've signed up as a user, all of my models have been replicated, one in the singular and the other in the plural.

Where might I instruct the MVC identity framework to utilise the already-existing (Singular) database objects rather than creating pluralized tables?

1
1
9/17/2016 7:50:36 PM

Accepted Answer

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); not apply to Identity tables. Therefore, it must be expressly resolved:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Configure Code First to ignore PluralizingTableName convention 
        // If you keep this convention then the generated tables will have pluralized names. 
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();

        modelBuilder.Entity<IdentityRole>().ToTable("AspNetRole");             
        modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRole");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogin");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaim");
        modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUser");

    }
3
9/19/2016 4:51:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow