一个人怎么可以把应用程序用户在相同的上下文对象的休息吗? [英] How can one put application users in the same context as the rest of the objects?
问题描述
股票asp.net mvc的5应用程序创建的应用程序的用户,在一个单独的上下文AKA标识用户,命名为一个名为IdentityModels.cs文件 - 它看起来像这样
The stock asp.net mvc 5 application creates the application users, aka identity users in a separate context, named a file called "IdentityModels.cs" - it looks like this
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
}
我试图把应用程序的用户在普通数据上下文,即像这样
I am attempting to put the Application users in a regular data context, i.e. something like this
public class BlogProphetContext : DbContext
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<Answer> Answers { get; set; }
public DbSet<Question> Questions { get; set; }
public DbSet<Tag> Tags { get; set; }
}
不过,每次我这样做,我每次碰到下面的错误时我尝试创建一个帐户
However, every time I do that I get the following error every time I try to create an account
The UserId field is required
在AccountController.cs当我尝试execure code的以下行
In the AccountController.cs when I attempt to execure the following line of code
result = await UserManager.AddLoginAsync(user.Id, info.Login);
我感觉我的做法是错误的,我不能在主数据上下文文件的ApplicationUsers没有某种形式的外部诈骗的 - 没有任何人知道的一些方法来做到这一点?所有的文件都是最新的。
I get the feeling my approach is wrong, and that I cannot have the ApplicationUsers in the main data context file without some sort of external chicanery - does anyone know of some way to do this? All of the files are up to date.
推荐答案
这是一个有点太容易了 - 事实证明,你必须这样做,删除
This was a bit too easy - it turns out that all you have to do it remove the
<ApplicationUser>
当你打电话的情况下,一切都变成了像你所期望的(即MVC假设和开发商的假设(在这种情况下,我的)同步。
when you call the context and everything turns out like you might expect (i.e. the MVC assumptions and the developers assumptions (in this case, mine) sync.
下面是正常工作
public class MyContext : IdentityDbContext
{
public MyContext()
: base("DefaultConnection")
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<Answer> Answers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
这篇关于一个人怎么可以把应用程序用户在相同的上下文对象的休息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!