Sto migrando la mia app in ASP.Net Core 2.0.
https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x
Nell'articolo sopra riportato, ha la seguente mappatura:
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);
}
Mi sta bene. Tuttavia, quando corro
dotnet ef migrations add InitialCreate
Ottengo il seguente errore:
Il tipo di entità 'IdentityUserRole' richiede una chiave primaria da definire.
ApplicationUser è definito come segue:
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; }
}
C'è qualcosa di sbagliato nella mappatura?
Prova ad aggiungere
builder.Entity<IdentityUserRole<int>>().HasKey(p => new { p.UserId, p.RoleId});