Resolving 'No key Defined' errors while using OnModelCreating with ApplicationDbContext?

后端 未结 3 760
我寻月下人不归
我寻月下人不归 2021-01-12 11:40

I\'ve been trying to create navigation properties for my collection types and I found this example of how one person accomplished it using OnModelCreating. I gave it a try i

相关标签:
3条回答
  • 2021-01-12 11:59

    You might have to add something like the following to OnModelCreating

    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
    

    See also:

    • Map tables using fluent api in asp.net MVC5 EF6?
    • User in Entity type MVC5 EF6
    0 讨论(0)
  • 2021-01-12 12:08

    I know this is a late response, and this may only work for people working off tutorials or school projects!

    For me, just deleting the database, .mdf, and migrations folder fixed it. Now, I don't know if you are using migrations, but this worked for me.

    I'm a novice, but what I've noticed is that Identity is very finicky with keys. Deleting the migration has helped with every (godforsaken) migration problem I've hit with my project.

    0 讨论(0)
  • 2021-01-12 12:21

    I think the issue is that you have removed the calling of base class from OnModelCreating. Try adding that also as shown below

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            //Your configuration goes here
        }
    
    0 讨论(0)
提交回复
热议问题