0

我遷移我的應用程序ASP.Net核2.0主鍵。遷移驗證到核2.0:實體類型「IdentityUserRole <int>」需要定義

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 

我得到以下錯誤:

The entity type 'IdentityUserRole' requires a primary key to be defined.

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

有什麼不對的映射?

+0

能否請您分享IdentityUser ** **角色類? –

+0

我沒有代碼。但是這裏是它的文檔。 https://docs.microsoft.com/en-us/aspnet/core/api/microsoft.aspnetcore.identity.entityframeworkcore.identityuserrole-1 –

+0

@JonasArcangel你最終搞清楚了這一點?我想轉出GUID主鍵爲int的。能夠在1.1,但不能在2.0。想想你'ApplicationUser'需要從'IdentityUser '這[GitHub的問題]繼承(https://github.com/aspnet/Identity/issues/780)可以幫助你,讓我接近,但沒有雪茄。 –

回答

0

嘗試增加

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

我仍然得到與此相同的錯誤。 –

+0

請嘗試以下操作: 'builder.Entity ()。HasKey(p => new {p.UserId,p.RoleId});' –

+0

IdentityUserRole需要一個類型參數<__>。 –

相關問題