Kye
Kye

Reputation: 4495

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.

Upvotes: 3

Views: 589

Answers (2)

DarthVader
DarthVader

Reputation: 55032

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.

Upvotes: 4

Martin Costello
Martin Costello

Reputation: 10863

Just sub-class the data context used for ASP.NET Identity.

Upvotes: 1

Related Questions