本文介绍了ASP.NET MVC Core:添加IdentityUser后出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习构建一个ASP.NET MVC Core Web应用程序。一切正常,直到我开始添加身份



注册用户可以创建许多作业。我使用用户的项目附带的 ApplicationUser.cs 。我为所有其他实体创建了另一个 DbContext



ApplicationUser.cs 在模型中:

  public class ApplicationUser:IdentityUser 
{
// CUSTOM PROPERTIES
public string Name {get;组; }
public ICollection< Job>工作{get;组; }
}

Job.cs 在模型中:

  public class Job 
{
public int ID {get;组; }
public string标题{get;组;

//包含其他属性

public string ApplicationUserID {get;组; }
public virtual ApplicationUser ApplicationUser {get;组; }
}

ApplicationDbContext.cs

  public class ApplicationDbContext:IdentityDbContext< ApplicationUser> 
{
public ApplicationDbContext(DbContextOptions< ApplicationDbContext> options):base(options)
{

}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}

code> Job.cs ,弹出错误。

  public string ApplicationUserID {get;组; } 
public virtual ApplicationUser ApplicationUser {get;组; }



我知道有一些线程讨论相同的错误,但是这些答案不能真正帮助。

解决方案

您的问题是两种不同的上下文。



JobDbContext 包含作业,它指的是 ApplicationUser ,这不是在这个上下文中,而是被EF自动添加,因为你链接到它。而这个 ApplicationUser (它的父 IdentityUser )包含登录集合( IdentityUserLogin ),这也不是这个上下文的一部分,而是由EF自动添加,等等...所有这些类都在 OnModelCreating 中配置 IdentityDbContext 并且未在 JobDbContext 中配置,这就是为什么你看到这个错误 - 你没有这个类的描述键/索引/引用 JobDbContext



您应该将两个DbContext组合成一个,或者删除 public virtual ApplicationUser ApplicationUser {get;组; } Job 类并手动管理相关对象(确保引用完整性,延迟加载等)。


I'm learning to build a ASP.NET MVC Core web application. Everything works fine until I start to add Identity to it.

Registered User can create many Jobs. I use the ApplicationUser.cs that comes with the project for the User. I created another DbContext for all others entities.

ApplicationUser.cs in Model:

public class ApplicationUser : IdentityUser
{
    // CUSTOM PROPERTIES
    public string Name { get; set; }
    public ICollection<Job> Jobs { get; set; }
}

Job.cs in Model:

public class Job 
{
    public int ID { get; set; }
    public string Title { get; set; }

    // contains other properties

    public string ApplicationUserID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

ApplicationDbContext.cs:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {

    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

When I add these 2 lines in Job.cs, the error pops up.

    public string ApplicationUserID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

I know that there are some threads discussing about the same error, but those answer can't really help.

解决方案

Your problem is two different contexts.

JobDbContext contains Job, which refers to ApplicationUser which is NOT in this context, but added by EF automatically because you linked to it. And this ApplicationUser (it's parent IdentityUser) contains collection of logins (IdentityUserLogin) which is not part of this context too, but auto-added by EF, and so on... All this classes are configured in OnModelCreating of IdentityDbContext and NOT configured in JobDbContext that's why you see this error - you have no description keys/indexes/references of this classes in JobDbContext.

You should either combine two DbContext into one, or remove public virtual ApplicationUser ApplicationUser { get; set; } from Job class and manage related object manually (ensure referential integrity, do lazy loading etc).

这篇关于ASP.NET MVC Core:添加IdentityUser后出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:24