[英]Single Context in ASP.NET MVC5 Entity Framework application
As demonstrated in my last question here: "Define the key for this EntityType." 正如我在上一个问题中所示: “定义此EntityType的键。” when including attribute of type ApplicationUser
当包含ApplicationUser类型的属性时
I need to use a single database context, whereas my current setup is one context (defined within IdentityModel.cs) for the login stuff and another context (defined externally) for all other database operations in the application. 我需要使用单个数据库上下文,而我当前的设置是登录内容的一个上下文(在IdentityModel.cs中定义)和应用程序中所有其他数据库操作的另一个上下文(在外部定义)。
How would I go about using a single context? 我将如何使用单一上下文? I do not care about existing data.
我不关心现有数据。
You can certainly use single context. 你当然可以使用单一的上下文。
Below is what i did: 以下是我的所作所为:
public class DataContext : IdentityDbContext<ApplicationUser>
{
public DataContext()
: base("DefaultConnection")
{
}
public IDbSet<Project> Projects { set; get; }
public IDbSet<Task> Tasks { set; get; }
}
Then in account controller: 然后在帐户控制器中:
public class AccountController : Controller
{
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DataContext())))
{
}
when you create a new project: 在创建新项目时:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
you get this which doesnt have much information, you can use this as well and add your dbsets. 你得到的这个没有太多的信息,你也可以使用它并添加你的dbsets。 It has a reference from AccountController.
它有来自AccountController的引用。
只是将用于ASP.NET Identity的数据上下文子类化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.