Identity DB context / hint no plural tables

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

Question

I have an existing DB context that worked well so far with EF configuration as below

   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>();
}

I followed an MSDN article to extend the Identity model provided by MVC to link to a person in my existing DB Context.

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; }

    }

The app works as long as I dont use the register or login function. However once I have registered as an user all my models are now duplicated, one as singular the other as a pluralized form of that.

Where could I tell the MVC identity framework not to recreate pluralized tables and use the existing (Singular) db objects?

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

Accepted Answer

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); doesn't work for Identity tables. So it is needed to resolve explicitly:

 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