Posted on Leave a comment



In the previous article we introduced the ApplicationDbContext class.

Search it in the project. You should find it within the IdentityModels.cs file. This class derives from the class that derives from IdentityDbContext DbContext.

DbContext is the connection class to a database. The class constructor has as a parameter the name of the connection string that we had previously found in the Web.config file.

public ApplicationDbContext() : base("DefaultConnection")

Let’s think about this information: in the Web.config you can put more connection strings, and then test the application on different databases, changing the parameter above, or you can create an application that connects to different databases by defining more than one DbContext.

Within a DbContext will be referenced all tables in the database we want to use in our application: or rather, all the tables that have constraints between them.

If for example we want to bind to the table of users created by Visual Studio, a second table created by us, the two tables should be part of the same DbContext. But we know nothing of the tables for the management of users created automatically. The class IdentityDbContext contains references to these tables. For this reason, our ApplicationDbContext not derived from DbContext but IdentityDBContext that contains, in fact, a little more information.

As you add tables for your application, you will work on ApplicationDbContext class. For a neater code, you go to create a new file in ApplicationDbContext.cs Models directory and copy, in this class, the ApplicationDbContext code (the only) which is IdentityModels.cs.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
     public ApplicationDbContext()
     : base("DefaultConnection")

     public static ApplicationDbContext Create()
       return new ApplicationDbContext();

Try running the application and perform a login to test that everything is working.

Leave a Reply

Your email address will not be published. Required fields are marked *