3

I would like to only have one DbContext in my ASP.NET MVC project. How should I merge the default IdentityDbContext with my own code first DbContext? They are using the same database.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("SignalRChat") { } } 

This is the default one provided in the IdentityModel.cs class.

Is it safe to do the following? And append my own DbSets?

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("SignalRChat") { } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } } 

Any advice is much appreciated!

1
  • 1
    Thats what I do in my projects. I then sit this and user manager, role manager etc inside a repository. I csn then unit test my bll such as a service and then integrate test my repository individually and also through service. Commented Jun 25, 2015 at 20:46

1 Answer 1

1

Yes, it is perfectly fine to add DbSet declarations in the DbContext classes. Normally you would maybe create a context for a related group of tables that makes sense when you access it from your repositories, e.g. ApplicationDbContext for user registration, login etc. another for Students and related entities.

If you have many contexts maybe you should check for the bounded context "pattern" https://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-B336#fbid=wH4n-081m_U.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.