Wing Kui Tsoi
Wing Kui Tsoi

Reputation: 544

ASP.NET MVC ApplicationDbContext Create

Can someone explain what is create method used for?

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
  public ApplicationDbContext()
    : base("DefaultConnection", throwIfV1Schema: false)
  {
  }

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

Upvotes: 2

Views: 1121

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32069

If you see the References of this Create static method you will find that this method has been used in ConfigureAuth method of the Startup partial class in Startup.Auth.cs file under App_start folder as follows:

public partial class Startup
{

    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);

        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Removed other codes for brevity
    }
}

Here CreatePerOwinContext registers a static callback which your application will use to get back a new instance of a specified type. This callback will be called once per request and will store the object/objects in OwinContext so that you will be able to use them throughout the application.

Here is more details with example.

Upvotes: 2

Related Questions