将Auth迁移到Core 2.0:实体类型'IdentityUserRole <int>'需要定义主键

时间:2017-09-14 02:51:26

标签: asp.net-core entity-framework-core asp.net-authentication

我正在将我的应用程序迁移到ASP.Net Core 2.0。

https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

在上面的文章中,它有以下映射:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    // Customize the ASP.NET Identity model and override the defaults if needed.
    // For example, you can rename the ASP.NET Identity table names and more.
    // Add your customizations after calling base.OnModelCreating(builder);

    builder.Entity<ApplicationUser>()
        .HasMany(e => e.Claims)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);

    builder.Entity<ApplicationUser>()
        .HasMany(e => e.Logins)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);

    builder.Entity<ApplicationUser>()
        .HasMany(e => e.Roles)
        .WithOne()
        .HasForeignKey(e => e.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Cascade);
}

对我来说很好看。但是,当我跑

dotnet ef migrations add InitialCreate

我收到以下错误:

  

实体类型'IdentityUserRole'需要主键   定义

ApplicationUser定义如下:

public class ApplicationUser : IdentityUser
{
    /// <summary>
    /// Navigation property for the roles this user belongs to.
    /// </summary>
    public virtual ICollection<IdentityUserRole<int>> Roles { get; } = new List<IdentityUserRole<int>>();

    /// <summary>
    /// Navigation property for the claims this user possesses.
    /// </summary>
    public virtual ICollection<IdentityUserClaim<int>> Claims { get; } = new List<IdentityUserClaim<int>>();

    /// <summary>
    /// Navigation property for this users login accounts.
    /// </summary>
    public virtual ICollection<IdentityUserLogin<int>> Logins { get; } = new List<IdentityUserLogin<int>>();

    public string FullName { get; set; }
}

映射有问题吗?

1 个答案:

答案 0 :(得分:0)

尝试添加

builder.Entity<IdentityUserRole<int>>().HasKey(p => new { p.UserId, p.RoleId});