EF代码首选迁移不需要的列IdentityRole_Id [英] EF Code First Migration unwanted column IdentityRole_Id

查看:135
本文介绍了EF代码首选迁移不需要的列IdentityRole_Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF 6.1.1
当我在我的数据库中使用代码优先迁移时,它会向AspNetUserRoles表添加两个不需要的列:IdentityRole_Id,IdentityUser_Id
我看到这个 RemoveFromRole无法正常工作,但没有帮助我。



我如何摆脱他们?



这是我的OnModelCreating

 {
如果(modelBuilder == null)
{
抛出新的ArgumentNullException(modelBuilder); $ protected override void OnModelCreating(DbModelBuilder modelBuilder)
}

//保存:
modelBuilder.Entity< IdentityUser>()。ToTable(AspNetUsers);

//将TUser更改为其他地方的ApplicationUser -
// IdentityUser和ApplicationUser基本上共享数据库中的AspNetUsers表:
EntityTypeConfiguration< ApplicationUser> table =
modelBuilder.Entity< ApplicationUser>()。ToTable(AspNetUsers);

table.Property((ApplicationUser u)=> u.UserName).IsRequired();

// EF不会让我们在ApplicationUserRole中交换IdentityUserRole:
modelBuilder.Entity< ApplicationUser>()。HasMany< IdentityUserRole>((ApplicationUser u)=> u。角色);
modelBuilder.Entity< IdentityUserRole>()。HasKey((IdentityUserRole r)=>
new {UserId = r.UserId,RoleId = r.RoleId})ToTable(AspNetUserRoles);

//单独留下:
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);

//添加这个,以便IdentityRole可以与ApplicationRole共享一个表:
modelBuilder.Entity< IdentityRole>()。ToTable(AspNetRoles);

//将这些从IdentityRole更改为ApplicationRole:
EntityTypeConfiguration< ApplicationRole> entityTypeConfiguration1 =
modelBuilder.Entity< ApplicationRole>()。ToTable(AspNetRoles);

entityTypeConfiguration1.Property((ApplicationRole r)=> r.Name).IsRequired();
}

它产生:

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


解决方案

假设您正在使用Identity EntityFramework(您的DbcontextClass:IdentityDbContext )然后我认为这一行是原因

  modelBuilder.Entity< IdentityUserRole>()。HasKey((IdentityUserRole r)=> ; 
new {UserId = r.UserId,RoleId = r.RoleId})。ToTable(AspNetUserRoles);

OnModelCreating中的所有代码将创建与默认相同的身份表(只要您的应用程序上下文类派生自 IdentityDbContext< ApplicationUser>



如果您想使用覆盖OnModelCreating的某种原因,例如



  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
}

那么你必须调用基数

  base.OnModelCreating(modelBuilder); 


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

解决方案

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

这篇关于EF代码首选迁移不需要的列IdentityRole_Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆