3

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.

1
  • Yeah, single Contexts as per DarthVader's answer definitely make migrations a lot simpler. Commented Feb 16, 2017 at 10:41

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.