繁体   English   中英

实体类型&#39;Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin <string> &#39;需要定义一个密钥

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

在介绍Identity之前,一切工作正常。

我将上下文类更改为:

public partial class VisualJobsDbContext : IdentityDbContext<ApplicationUser>
{
    private IConfigurationRoot _config;

    public VisualJobsDbContext() { }

    public VisualJobsDbContext(IConfigurationRoot config, DbContextOptions options) : base(options)
    {
        _config = config;
    }...

我有一个通用存储库,该存储库以以下形式声明:

public abstract class GenericRepository<C,T> : IGenericRepository<T> where T : class where C : IdentityDbContext<ApplicationUser>, new() 
{
    private C _entities = new C();
    private VisualJobsDbContext context;
    private DbSet<T> dbSet;

    public GenericRepository(VisualJobsDbContext context)
    {
        this.context = context;
        this.dbSet = context.Set<T>();
    }....

错误发生在

this.dbSet = context.Set <>();

第一次被击中。 我的OnModelCreating方法确实被调用了。 我想从我的MVC应用程序访问的页面不需要登录,因为它是常规页面,但是有些区域需要登录。 但是,在错误发生之前,我并没有深入了解我的控制器Constructor方法。 要注册服务,我有:

services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<VisualJobsDbContext>()
            .AddDefaultTokenProviders();

我可以迈出这一步。

有什么想法我想念的吗? 我已经看过这个stackoverflow答案和其他答案 ,但这并没有真正的帮助

在我的OnModelCreating方法中。 我需要放置base.OnModelCreating(modelBuilder); 作为我的第一行代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM