使用 EF 数据库优先 MVC5 的 ASP.NET 身份
是否可以将新的 Asp.net Identity 与 Database First 和 EDMX 一起使用?还是先用代码?
Is it possible to use the new Asp.net Identity with Database First and EDMX? Or only with code first?
这是我所做的:
1) 我创建了一个新的 MVC5 项目,并让新的身份在我的数据库中创建了新的用户和角色表.
1) I made a new MVC5 Project and had the new Identity create the new User and Roles tables in my database.
2) 然后我打开我的 Database First EDMX 文件并拖入新的 Identity Users 表中,因为我还有其他与之相关的表.
2) I then opened my Database First EDMX file and dragged in the new Identity Users table since I have other tables that relate to it.
3) 保存 EDMX 后,Database First POCO 生成器将自动创建一个用户类.但是,UserManager 和 RoleManager 需要从新的 Identity 命名空间 (Microsoft.AspNet.Identity.IUser) 继承的 User 类,因此无法使用 POCO User 类.
3) Upon saving the EDMX, the Database First POCO generator will auto create a User class. However, UserManager and RoleManager expects a User class inheriting from the new Identity namespace (Microsoft.AspNet.Identity.IUser), so using the POCO User class won't work.
我想一个可能的解决方案是编辑我的 POCO 生成类以使我的用户类继承自 IUser?
I guess a possible solution is to edit my POCO Generation Classes to have my User class inherit from IUser?
还是 ASP.NET Identity 仅与 Code First Design 兼容?
Or is ASP.NET Identity only compatible with Code First Design?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
更新:按照下面安德斯·阿贝尔的建议,这就是我所做的.它有效,但我想知道是否有更优雅的解决方案.
Update: Following Anders Abel's suggestion below, this is what I did. It work's, but I'm wondering if there is a more elegant solution.
1) 我通过在与自动生成的实体相同的命名空间中创建分部类来扩展我的实体 User 类.
1) I extended my entity User class by creating a partial class within the same namespace as my auto generated entities.
namespace MVC5.DBFirst.Entity
{
public partial class AspNetUser : IdentityUser
{
}
}
2) 我将我的 DataContext 更改为继承自 IdentityDBContext 而不是 DBContext.请注意,每次更新 EDMX 并重新生成 DBContext 和 Entity 类时,您都必须将其重新设置为此.
2) I changed my DataContext to inherit from IdentityDBContext instead of DBContext. Note that every time you update your EDMX and regenerate the DBContext and Entity classes, you'll have to set this back to this.
public partial class MVC5Test_DBEntities : IdentityDbContext<AspNetUser> //DbContext
3) 在自动生成的 User 实体类中,您必须将 override 关键字添加到以下 4 个字段或将这些字段注释掉,因为它们是从 IdentityUser 继承的(步骤 1).请注意,每次更新 EDMX 并重新生成 DBContext 和 Entity 类时,您都必须将其重新设置为此.
3) Within your auto generated User entity class, you must add the override keyword to the following 4 fields or comment these fields out since they are inherited from IdentityUser (Step 1). Note that every time you update your EDMX and regenerate the DBContext and Entity classes, you'll have to set this back to this.
override public string Id { get; set; }
override public string UserName { get; set; }
override public string PasswordHash { get; set; }
override public string SecurityStamp { get; set; }
应该可以将身份系统与 POCO 和 Database First 一起使用,但您必须进行一些调整:
It should be possible to use the identity system with POCO and Database First, but you'll have to make a couple of tweaks:
- 更新用于 POCO 生成的 .tt 文件,使实体类成为
partial
.这将使您可以在单独的文件中提供额外的实现. - 在另一个文件中对
User
类进行部分实现
- Update the .tt-file for POCO generation to make the entity classes
partial
. That will make it possible for you to supply additional implementation in a separate file. - Make a partial implementation of the
User
class in another file
partial User : IUser
{
}
这将使 User
类实现正确的接口,而不会触及实际生成的文件(编辑生成的文件总是一个坏主意).
That will make the User
class implement the right interface, without touching the actual generated files (editing generated files is always a bad idea).