ASP.NET MVC ApplicationDbContext 创建
ASP.NET MVC ApplicationDbContext Create
谁能解释一下创建方法的用途?
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
如果您查看此 Create
静态方法的引用,您会发现此方法已在 Startup
部分 class 的 ConfigureAuth
方法中使用 Startup.Auth.cs
文件夹下的App_start
文件如下:
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
}
}
这里 CreatePerOwinContext 注册一个静态回调,您的应用程序将使用它来取回指定类型的新实例。
每个请求都会调用一次此回调,并将 object/objects 存储在 OwinContext 中,以便您可以在整个应用程序中使用它们。
Here is more details with example.
谁能解释一下创建方法的用途?
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
如果您查看此 Create
静态方法的引用,您会发现此方法已在 Startup
部分 class 的 ConfigureAuth
方法中使用 Startup.Auth.cs
文件夹下的App_start
文件如下:
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
}
}
这里 CreatePerOwinContext 注册一个静态回调,您的应用程序将使用它来取回指定类型的新实例。 每个请求都会调用一次此回调,并将 object/objects 存储在 OwinContext 中,以便您可以在整个应用程序中使用它们。
Here is more details with example.