Reputation: 360
I'm using EF 6.1.1 When i use code-first migration on my DB, it adds two unwanted columns to the AspNetUserRoles table: IdentityRole_Id, IdentityUser_Id I've seen this RemoveFromRole cannot work as expected, but didnt help me.
how do i get rid of them ??
this is my OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException("modelBuilder");
}
// Keep this:
modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");
// Change TUser to ApplicationUser everywhere else -
// IdentityUser and ApplicationUser essentially 'share' the AspNetUsers Table in the database:
EntityTypeConfiguration<ApplicationUser> table =
modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");
table.Property((ApplicationUser u) => u.UserName).IsRequired();
// EF won't let us swap out IdentityUserRole for ApplicationUserRole here:
modelBuilder.Entity<ApplicationUser>().HasMany<IdentityUserRole>((ApplicationUser u) => u.Roles);
modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");
// Leave this alone:
EntityTypeConfiguration<IdentityUserLogin> entityTypeConfiguration =
modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) =>
new
{
UserId = l.UserId,
LoginProvider = l.LoginProvider,
ProviderKey
= l.ProviderKey
}).ToTable("AspNetUserLogins");
//entityTypeConfiguration.HasRequired<IdentityUser>((IdentityUserLogin u) => u.User);
EntityTypeConfiguration<IdentityUserClaim> table1 = modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
//table1.HasRequired<IdentityUser>((IdentityUserClaim u) => u.User);
// Add this, so that IdentityRole can share a table with ApplicationRole:
modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
// Change these from IdentityRole to ApplicationRole:
EntityTypeConfiguration<ApplicationRole> entityTypeConfiguration1 =
modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");
entityTypeConfiguration1.Property((ApplicationRole r) => r.Name).IsRequired();
}
and it produces this:
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
IdentityRole_Id = c.String(maxLength: 128),
IdentityUser_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.IdentityRole_Id)
.ForeignKey("dbo.AspNetUsers", t => t.IdentityUser_Id)
.Index(t => t.IdentityRole_Id)
.Index(t => t.IdentityUser_Id);
Upvotes: 2
Views: 1436
Reputation: 20383
Assuming you are using Identity EntityFramework (your DbcontextClass : IdentityDbContext) then I think this line is the reason
modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");
All the code in your OnModelCreating will create the same identity tables as default (as long as your application context class derives from IdentityDbContext<ApplicationUser>
If you want to use an override OnModelCreating for some reason for example
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
then you have to call the base
base.OnModelCreating(modelBuilder);
Upvotes: 2