简体   繁体   English

ASP.NET MVC5实体框架应用程序中的单个上下文

[英]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.

相关问题 ASP.NET MVC5 和实体框架设计题 - ASP.NET MVC5 and Entity Framework design questions 使用实体框架和ASP.NET MVC5更新记录 - Update records using Entity Framework and ASP.NET MVC5 多层ASP.NET MVC应用程序中的实体框架上下文 - Entity Framework Context in a multi-tier ASP.NET MVC Application 在 LINQ Entity Framework asp.net MVC 5 中切换数据库上下文 - Switching DB Context in LINQ Entity Framework asp.net MVC 5 NuGet 包 (.NET Standard 2.x) 破坏 ASP.NET MVC5 应用程序(.NET 完整框架) - NuGet package (.NET Standard 2.x) breaking ASP.NET MVC5 application (.NET full framework) 与单表实体框架asp.net MVC的多个关系 - Multiple relationships to single table Entity Framework asp.net MVC 我如何使用实体框架ASP.NET MVC5保存相关数据? - How do i save related data using entity framework asp.net mvc5? 仅在主页命中时触发连接池-ASP.NET MVC5实体框架 - Connection Pooling only triggered on home page hit - ASP.NET MVC5 Entity Framework ASP.NET MVC5-如何强制实体框架忽略模型中的属性? - ASP.NET MVC5 - How to enforce Entity Framework to ignore properties in model? ASP.NET MVC5实体框架6 get bool = true和bool = false LINQ - ASP.NET MVC5 Entity Framework 6 get bool = true and bool = false LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM