ASP.NET身份2:创建Fluent Code First关系时出现问题

时间:2014-07-27 05:05:05

标签: entity-framework code-first

我正在尝试从头开始创建一个ASP.NET Identity DbContext(摆脱IdentityDbContext< ...>的依赖性)

几乎就在那里......但我的EF Code First流畅的关系还不对。

表结构出现: enter image description here

但更像是以下内容:

enter image description here

即使我明确定义了HasForeignKey之后也是如此。

我的问题是......我在关系方面出了什么问题?

谢谢!

我正在使用的映射如下:

public class ApplicationDbContext : DbContext
{
    ...
    public ApplicationDbContext()
        : base("DefaultConnection") //, throwIfV1Schema: false
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }
    ...
    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new IdentityRoleMap());
        modelBuilder.Configurations.Add(new IdentityUserClaimMap());
        modelBuilder.Configurations.Add(new IdentityUserLoginMap());
        modelBuilder.Configurations.Add(new IdentityUserMap());
        modelBuilder.Configurations.Add(new IdentityUserRoleMap());

    }
    ...
}
public class IdentityUserClaimMap : EntityTypeConfiguration<IdentityUserClaim>
{
    public IdentityUserClaimMap()
    {
        this.HasKey(t => t.Id);
        this.Property(t => t.UserId).IsRequired().HasMaxLength(128);
        this.ToTable("Custom_UserClaims");
    }
}
public class IdentityUserLoginMap : EntityTypeConfiguration<IdentityUserLogin>
{
    public IdentityUserLoginMap()
    {
        this.HasKey(t => new { t.UserId, t.LoginProvider, t.ProviderKey });
        this.Property(t => t.LoginProvider).IsRequired().HasMaxLength(128);
        this.Property(t => t.ProviderKey).IsRequired().HasMaxLength(128);
        this.Property(t => t.UserId).IsRequired().HasMaxLength(128);
        this.ToTable("Custom_UserLogins");
    }
}
public class IdentityRoleMap : EntityTypeConfiguration<IdentityRole>
{
    public IdentityRoleMap()
    {
        this.HasKey(t => t.Id);
        this.Property(t => t.Id).IsRequired().HasMaxLength(128);
        this.Property(t => t.Name).IsRequired().HasMaxLength(256);
        this.ToTable("Custom_Roles");
        this
            .HasMany(x => x.Users)
            .WithOptional()
            .HasForeignKey(x => x.RoleId);

    }
}

public class IdentityUserMap : EntityTypeConfiguration<ApplicationUser>
{
    public IdentityUserMap()
    {
        this.HasKey(t => t.Id);
        this.Property(t => t.Id).IsRequired().HasMaxLength(128);
        this.Property(t => t.Email).HasMaxLength(256);
        this.Property(t => t.UserName).IsRequired().HasMaxLength(256);
        this.ToTable("Custom_Users");

        //Nav:Claims: User can have 1-(0-*) claims
        this
            .HasMany<IdentityUserClaim>(x => x.Claims)
            .WithRequired()
            .HasForeignKey(x => x.UserId)
            .WillCascadeOnDelete();

        //Nav:Logins: User can have 1-(0-*) Logins
        this
            .HasMany<IdentityUserLogin>( x => x.Logins)
            .WithRequired()
            .HasForeignKey(x => x.UserId)
            .WillCascadeOnDelete();

        //Nav:Roles:
        //Interestingly, appears not to be using a *-* Map statement:
        this
            .HasMany<IdentityUserRole>(x => x.Roles)
            .WithRequired()
            .HasForeignKey(x => x.UserId)
            .WillCascadeOnDelete(false);
    }
}
public class IdentityUserRoleMap : EntityTypeConfiguration<IdentityUserRole>
{
    public IdentityUserRoleMap()
    {
        this
            .ToTable("Custom_UserRoles")
            .HasKey(x => new {x.UserId, x.RoleId});

    }
}

1 个答案:

答案 0 :(得分:0)

哦,这很烦人。

原来它工作正常。

我没有那么多地使用Sql Server的图表工具,并且没有意识到在某些时候我做对了...但是图表一直都是错的,只是因为我之前没有刷新表格我选择了他们。真是浪费时间!

关于是否使用一个或两个DbContexts作为Erik在评论中建议,我将进一步考虑。现在,我将使用单个DbContext,因为所有功能似乎都运行正常。

晚。