默认的MVC 5应用程序在IdentityModels中附带了这段代码.cs-这段代码适用于所有ASP.默认模板的NET标识操作:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
如果我使用实体框架视图构建一个新的控制器,并创建一个"新的数据上下文…"在对话框中,我会生成以下内容:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class AllTheOtherStuffDbContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
{
}
public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
}
}
如果我使用EF构建另一个控制器+视图,例如,对于动物模型,这一新行将在public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
以下自动生成-如下所示:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class AllTheOtherStuffDbContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
{
}
public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
public System.Data.Entity.DbSet<WebApplication1.Models.Animal> Animals { get; set; }
}
}
ApplicationDbContext
(对于所有ASP.NET身份信息)继承自IdentityDbContext
,而IdentityDbContext
又继承自DbContext
.
所以我的问题是:
这两个(ApplicationDbContext
和AllOtherStuffDbContext
)中的哪一个应该用于我自己的所有其他模型?或者我应该只使用默认的autogenerated ApplicationDbContext
,因为使用它应该不会有问题,因为它派生自基类DbContext
,还是会有一些开销?你应该只在你的应用程序中为你的所有模型使用一个DbContext
个对象(我读过这个地方),所以我不应该考虑在一个应用程序中同时使用ApplicationDbContext
和AllOtherStuffDbContext
.或者什么是MVC5与ASP的最佳实践.网络身份?