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:
ApplicationDbContext
class defined in IdentityModels.cs
with my own?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!
Is it okay to use just one database context for my entire application, like I'm doing now?
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?
UserStore
which receives DbContext
as an argument in constructor so thisvar 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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With