IdentityUserLogin <string> 'requires a primary key to determine the error when adding migration

I use 2 different Dbcontexts. I want to use 2 different database users and mycontext. In doing so, I get an error message. The object type "Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin" requires a primary key definition. I think something is wrong with IdentityUser, please suggest me where I can change my code to add migration.

My Dbcontext class:

class MyContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Tag) .WithMany(t => t.PostTags) .HasForeignKey(pt => pt.TagId); } } public class Post { public int PostId { get; set; } public string Title { get; set; } public AppUser User {get; set;} public string Content { get; set; } public List<PostTag> PostTags { get; set; } } public class Tag { public string TagId { get; set; } public List<PostTag> PostTags { get; set; } } public class PostTag { public int PostId { get; set; } public Post Post { get; set; } public string TagId { get; set; } public Tag Tag { get; set; } } 

and class AppUser:

 public class AppUser : IdentityUser { //some other propeties } 

when I try to add migration, the following error occurs.

 The entity type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin<string>' requires a primary key to be defined. 

give me a solution to solve the problem.

+6
source share
3 answers

To reduce the link to a short link, try the following:

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); ... 

See the link above for more details.

+14
source

The problem is that AppUser inherits from IdentityUser, and their primary keys are not displayed in the OnModelCreating dbcontext method.

There is already a permission message. Follow the link below.

EntityType 'IdentityUserLogin' does not have a specific key. Define a key for this EntityType

Hope this helps.

+5
source

Yes! Addendum:

  base.OnModelCreating(modelBuilder); 

Solved all my problems! Now I am a happy person.

+1
source

All Articles