Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing automatically created ApplicationDbContext

I should preface this with a disclaimer saying I'm new to ASP.NET development and don't really understand database contexts, despite spending the last hour reading documentation. When I built my ASP.NET MVC 5 application I chose to have individual user account authentication. Visual Studio created a file called IdentityModels.cs, and within there it defined an ApplicationUser class and a ApplicationDbContext class.

I've done some development work, and my CRUD controllers use ApplicationDbContext to talk to the database, by having this private property on every controller:

private ApplicationDbContext db = new ApplicationDbContext();

In the controller actions I then do things like:

return View(db.Trains.ToList());

I want to tidy this database context stuff up, but I need to understand it first. My main questions are:

  1. Is it okay to use just one database context for my entire application, like I'm doing now?
  2. Can I replace the ApplicationDbContext class defined in IdentityModels.cs with my own?
  3. The ApplicationDbContext class derives from IdentityDbContext<ApplicationUser>, does that mean I should have seperate database contexts for my user authentication stuff provided by Visual Studio, and my own code?

I think my end goal is to use my own database context, called DatabaseContext, which is then used in a base controller that all of my controllers inherit from. Then I only have one place where the database context is instantiated, instead of within every controller.

Who knows, I may be thinking the wrong way about this. Everybody seems to have their own preferred way of dealing with this.

Thank you!

like image 505
John Dorean Avatar asked Jan 18 '15 13:01

John Dorean


1 Answers

Is it okay to use just one database context for my entire application, like I'm doing now?

  • If you decided that you are going to have access to DB directly from UI layer (Which is a separate discussion) than it is OK, since ApplicationDbContext is a private field of your controller and controllers are created and disposed per request - ApplicationDbContext will be created and disposed per request.

Can I replace the ApplicationDbContext class defined in IdentityModels.cs with my own?

  • You definitely can do it. It is used to create a UserStore which receives DbContext as an argument in constructor so this

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new CustomDbContext("connection")));

will work. You still have to make sure that ApplicationUser is an entity in your custom context. Of course you can override/replace ApplicationUser as well.

The ApplicationDbContext class derives from IdentityDbContext, does that mean I should have seperate database contexts for my user authentication stuff provided by Visual Studio, and my own code?

By default Asp.Net Identity generates a new data base for you and ApplicationDbContext configured to work with this data base. You can store your authentication related entities in any other data base as well, you just need to make sure that all the related tables are there.You can also extend this data base to include other tables that you are using in your application so you could use the same context all over.

P.S: ApplicationDbContext doesn't have to implement IdentityDbContext<ApplicationUser>, Extending a default DbContext works as well (if you already generated a Db you will have to update it\use code migrations for the following to work):

 public class ApplicationDbContext : DbContext 
  {
     public ApplicationDbContext()
         : base("DefaultConnection")
     {
     }

     public static ApplicationDbContext Create()
     {
         return new ApplicationDbContext();
     } 
  }
like image 159
Alex Art. Avatar answered Oct 14 '22 05:10

Alex Art.