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

瘦欲@ 提交于 2019-12-01 04:44:36
jjj

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:

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
    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!