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:
- Is it okay to use just one database context for my entire application, like I'm doing now?
- Can I replace the
ApplicationDbContextclass defined inIdentityModels.cswith my own? - The
ApplicationDbContextclass derives fromIdentityDbContext<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!