98

I am using 2 different Dbcontexts. i want to use 2 different databases users and mycontext. While doing that i am getting a error The entity type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin' requires a primary key to be defined. I think there is something wrong with IdentityUser please suggest me where can i change my code so that i can 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 AppUser class:

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 solution to resolve the issue..

Ramesh Kumar
  • 1,241
  • 1
  • 9
  • 15

3 Answers3

267

To reduce the link to a nutshell, try this:

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

    ...

See Above link for more.

Greg Gum
  • 33,478
  • 39
  • 162
  • 233
  • 4
    Had this problem with Core 3.0 extending ASPNetUsers but with a single DB. added this line to ApplicationDBContext.OnModelCreating and it worked a charm. – BrownPony Dec 13 '19 at 02:10
  • Does it matter where in the override of OnModelCreating you call base.OnModelCreating (i.e. does it need to be the first thing you call)? – mark_h May 01 '20 at 15:17
  • I had to use "ModelBuilder" instead of "DbModelBuilder" for .NET Core – BlueSky Oct 06 '21 at 05:44
  • Great, I was supposed to use base because my CustomDbContext was inherited from IdentityDbContext – Tấn Nguyên Jun 13 '22 at 04:28
40

This issue will start coming as soon as you wrote the following lines in DBContext without adding 'base.OnModelCreating(modelBuilder);'

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

}

Two solutions:

1) Don't override OnModelCreating in DbContext Until it becomes necessary

2) Override but call base.OnModelCreating(modelBuilder)

TheKingPinMirza
  • 7,924
  • 6
  • 51
  • 81
8

The problem is AppUser is inherited from IdentityUser and their primary keys are not mapped in the method OnModelCreating of dbcontext.

There is already a post available with resolution. Visit the below link

EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

Hope this helps.

Community
  • 1
  • 1
Cinchoo
  • 6,088
  • 2
  • 19
  • 34