【问题标题】:ASP.NET-Identity OnModelCreating Modelbuilder Generating Duplicate Foreign KeysASP.NET-Identity OnModelCreating Modelbuilder 生成重复的外键
【发布时间】:2016-07-13 11:17:38
【问题描述】:

我有一个 ASP MVC 5 应用程序,但模型构建器正在为 IdentityUserRole、IdentityUserClaim 和 IdentityUserLogin 表创建重复的外键。

例如,在下面生成的迁移表中,有 RoleId 和 IdentityRole_ID

CreateTable(
            "dbo.UserRole",
            c => new
                {
                    RoleId = c.String(nullable: false, maxLength: 128),
                    UserId = c.String(nullable: false, maxLength: 128),
                    IdentityRole_Id = c.String(maxLength: 128),
                    ApplicationUser_Id = c.String(maxLength: 128),
                })
            .PrimaryKey(t => new { t.RoleId, t.UserId })
            .ForeignKey("dbo.Role", t => t.IdentityRole_Id)
            .ForeignKey("dbo.User", t => t.ApplicationUser_Id)
            .Index(t => t.IdentityRole_Id)
            .Index(t => t.ApplicationUser_Id);

在我的 fluent api 中,我将其定义如下:

   public IdentityUserRoleConfiguration()
    {
        HasKey(x => new { x.RoleId, x.UserId });
        ToTable("UserRole");
    }

我的模型构建器类:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    // Add some DBSets      

    public ApplicationDbContext()
        : base("ApplicationDbContext", throwIfV1Schema: false)
    {
    }

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

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

        // base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());
        modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration());
        modelBuilder.Configurations.Add(new IdentityRoleConfiguration());
        modelBuilder.Configurations.Add(new IdentityUserClaimConfiguration());
        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());


    }

}

我已注释掉 base.OnModelCreating(modelBuilder); 语句,因为它在我运行迁移时会引发以下错误:

A configuration for type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole' has already been added. To reference the existing configuration use the Entity<T>() or ComplexType<T>() methods.

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-identity ef-fluent-api


    【解决方案1】:

    配置可以实现如下,

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<IdentityRole>().ToTable("Roles");
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLoginProviders");
            modelBuilder.Configurations.Add(new UserConfiguration());
        }
    

    由于Microsoft.AspNet.Identity.EntityFramework中的大多数IdentityRole、IdentityUserRole、IdentityUserClaim、IdentityUserLogin模型已经实现了流畅的配置,因此它只允许覆盖ApplicationUser。因此,实现他的唯一方法是Entity&lt;T&gt;()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 2015-04-13
      相关资源
      最近更新 更多