简体   繁体   中英

Single Context in ASP.NET MVC5 Entity Framework application

As demonstrated in my last question here: "Define the key for this EntityType." when including attribute of type 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.

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. It has a reference from AccountController.

只是将用于ASP.NET Identity的数据上下文子类化。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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