2

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();
  }
}
1
  • 1
    It is used to create your own DB Context to use. Please refer to answers to this SO question --> Link Commented Jan 23, 2019 at 5:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I got it creates an new dbcontext instance will be stored in the OwinContext.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.