默认的MVC 5 App附带了IdentityModels.cs中的这段代码 - 这段代码用于默认模板的所有ASP.NET Identity操作:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
如果我使用带有Entity Framework的视图构建一个新控制器并在对话框中创建一个“新数据上下文...”,我会为我生成这个:
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来构建另一个控制器视图,例如对于一个Animal模型,这个新行将在 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
. AllOtherStuffDbContext
(对于我自己的东西)继承自 DbContext
.
所以我的问题是:
我应该为所有其他自己的模型使用这两个中的哪一个( ApplicationDbContext
和 AllOtherStuffDbContext
)?或者我应该使用默认自动生成的 ApplicationDbContext
,因为它不应该是使用它的问题,因为它派生自基类 DbContext
,还是会有一些开销?你应该在你的应用程序中只为你的所有模型使用一个 DbContext
对象(我已在某处读过)所以我甚至不应该考虑在一个应用程序中同时使用 ApplicationDbContext
和 AllOtherStuffDbContext
?或者使用ASP.NET身份的MVC 5中的最佳实践是什么?
4 回答
有很多关于 IdentityDbContext 的混淆,在Stackoverflow中快速搜索,你会发现这些问题:
“Why is Asp.Net Identity IdentityDbContext a Black-Box?
How can I change the table names when using Visual Studio 2013 AspNet Identity?
Merge MyDbContext with IdentityDbContext“
要回答所有这些问题,我们只需要了解 IdentityDbContext 只是一个继承自DbContext的类 .
我们来看看IdentityDbContext source:
基于源代码,如果我们想将IdentityDbContext与我们的DbContext合并,我们有两个选择:
First Option:
创建一个DbContext,它继承自IdentityDbContext并可以访问这些类 .
Extra Notes:
1)我们还可以使用以下解决方案更改asp.net Identity默认表名:
2)此外,我们可以扩展每个类并将任何属性添加到类'IdentityUser','IdentityRole',...
为了节省时间,我们可以使用AspNet Identity 2.0 Extensible Project Template来扩展所有类 .
Second Option: (不推荐)
如果我们自己编写所有代码,我们实际上不必继承IdentityDbContext .
所以基本上我们可以继承DbContext并从IdentityDbContext source code实现"OnModelCreating(ModelBuilder builder)"的自定义版本
如果你深入了解IdentityDbContext的抽象,你可以回答这个问题,但是如果你想要更多地控制创建的内容,那么对Identity包的依赖性要小一些have a look at my question and answer here . 如果您按照链接有一个代码示例,但总的来说,您只需将所需的DbSet添加到您自己的DbContext子类中 .
对于大家来说这是一个迟到的条目,但下面是我的实现 . 您还会注意到我已经删除了更改KEY默认类型的功能:有关详细信息,请参阅以下文章:
Extending Identity Models and Using Integer Keys Instead of Strings
Change Primary Key for Users in ASP.NET Identity
NOTES:
请注意,您不能使用
Guid's
作为密钥 . 这是因为在引擎盖下它们是Struct
,因此没有取消装箱,这将允许它们从通用的<TKey>
参数转换 .THE CLASSES LOOK LIKE:
我会使用一个继承自IdentityDbContext的Context类 . 通过这种方式,您可以让上下文了解您的类与IdentityUser和IdentityDbContext的角色之间的任何关系 . IdentityDbContext中的开销非常小,它基本上是一个带有两个DbSet的常规DbContext . 一个用户,一个用于角色 .