问题
I scaffolded my database with EF and I changed the mydbcontext to inherit from IdentityDbContext because I want everything under one dbcontext.
The problem is that when I start the application and write the credentials in the login page, I get an exception:
The entity type 'AspNetUserLogins' requires a primary key to be defined
which I didn't get before, because I am calling the base.OnModelCreating(modelBuilder); What am I doing wrong?
public partial class AdventuresContext : IdentityDbContext<ApplicationUser>
{
public AdventuresContext()
{
}
public AdventuresContext(DbContextOptions<AdventuresContext> options)
: base(options)
{
}
public virtual DbSet<Adventures> Adventures { get; set; }
public virtual DbSet<AspNetRoleClaims> AspNetRoleClaims { get; set; }
public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUserRoles> AspNetUserRoles { get; set; }
public virtual DbSet<AspNetUserTokens> AspNetUserTokens { get; set; }
public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Adventures");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
modelBuilder.Entity<Adventures>(entity =>
{
entity.HasIndex(e => e.UserId);
entity.Property(e => e.CountryCodeIso03from).HasColumnName("CountryCodeISO03From");
entity.Property(e => e.CountryCodeIso03to).HasColumnName("CountryCodeISO03To");
entity.Property(e => e.Name).HasMaxLength(50);
entity.Property(e => e.ShortDescription).HasMaxLength(128);
entity.Property(e => e.Timestamp).IsRowVersion();
entity.Property(e => e.UserId).IsRequired();
entity.HasOne(d => d.User)
.WithMany(p => p.Adventures)
.HasForeignKey(d => d.UserId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Adventures_Adventures");
});
}
}
回答1:
I believe there are two ways to fix this.
You could add the [Key] on a public property in your AspNetUserLogins class with a getter and setter, for example:
[Key]
public int SomeFieldName {get;set;}
Or you could use your OnModelCreating override to call HasNoKey(), for example:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<AspNetUserLogins>(o =>
{
o.HasNoKey();
});
}
回答2:
The AspNetUsersLogins is the standard class that comes with Identity. It has 2 primary keys, which are LoginProvider and ProviderKey. I tried to change them from string to Guid but still the same message. I also tried to put on both of them the [Key] attribue and i have an exception that says: "InvalidOperationException: Entity type 'AspNetUserLogins' has composite primary key defined with data annotations. To set composite primary key, use fluent API.". Any other ideas?The AspNetUsersLogins that EF scaffolded looks like this:
public partial class AspNetUserLogins
{
public string LoginProvider { get; set; }
public string ProviderKey { get; set; }
public string ProviderDisplayName { get; set; }
public string UserId { get; set; }
public virtual AspNetUsers User { get; set; }
}
回答3:
Since you inherit from IdentityDbContext, there is no need for you to recreate AspNet* DbSet,I suggest you remove all the built-in DbSet<AspNet*> and modelBuilder.Entity<AspNet*>.
public partial class AdventuresContext : IdentityDbContext<ApplicationUser>
{
public AdventuresContext ()
{
}
public AdventuresContext (DbContextOptions<AdventuresContext> options)
: base(options)
{
}
public virtual DbSet<Adventures> Adventures { get; set; }
//remove below DbSet
//public virtual DbSet<AspNetRoleClaims> AspNetRoleClaims { get; set; }
//public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
//public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
//public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
//public virtual DbSet<AspNetUserRoles> AspNetUserRoles { get; set; }
//public virtual DbSet<AspNetUserTokens> AspNetUserTokens { get; set; }
//public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Adventures");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//...
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
var conn = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Adventures";
services.AddDbContext<AdventuresContext>(options =>
options.UseSqlServer(conn));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<AdventuresContext>();
}
来源:https://stackoverflow.com/questions/59461666/the-entity-type-aspnetuserlogins-requires-a-primary-key-to-be-defined