ASP.NET身份与EF数据库优先MVC5 [英] ASP.NET Identity with EF Database First MVC5

查看:150
本文介绍了ASP.NET身份与EF数据库优先MVC5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用新的Asp.net的身份与数据库优先和EDMX?或只与code第一?

下面是我所做的:

1)我做了一个新的项目MVC5并且有新的身份在我的数据库中创建新的用户和角色表。

2)然后我打开我的数据库首先EDMX文件和新用户的身份拖累表,因为我有,涉及到它的其他表。

3)保存的EDMX,数据库首先POCO发电机将自动创建一个User类。然而,和的UserManager预计RoleManager用户从类新标识命名空间(Microsoft.AspNet.Identity.IUser)继承,因此,使用POCO类的用户将无法正常工作。

我想一个可能的解决方法是编辑我的POCO代班有来自IUSER我的User类继承?

或者是ASP.NET身份只有code首先设计?

兼容

+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++

更新:继以下安德斯亚伯的建议,这是我做的。它工作的,但我不知道是否有一个更优雅的解决方案。

1)我用我的自动生成的实体相同的命名空间中创建一个分部类扩展我的实体User类。

 命名空间MVC5.DBFirst.Entity
{
    公共部分类AspNetUser:IdentityUser
    {
    }
}

2)我改变了我的DataContext从IdentityDBContext,而不是继承的DbContext。请注意,每次更新您的EDMX并重新生成的DbContext和实体类的时候,你就必须设置这个回本。

 公共部分类MVC5Test_DBEntities:IdentityDbContext< AspNetUser> //的DbContext

3)在你的自动生成的用户的实体类,则必须override关键字添加到以下4个领域或因为它们是从IdentityUser(步骤1)评论继承这些领域了。请注意,每次更新您的EDMX并重新生成的DbContext和实体类的时候,你就必须设置这个回本。

 覆盖公共字符串ID {搞定;组; }
    覆盖公共字符串用户名{获得;组; }
    覆盖公共字符串PasswordHash {搞定;组; }
    覆盖公共字符串SecurityStamp {搞定;组; }


解决方案

应该是可以使用的标识系统,POCO和数据库第一次,但你必须做一些调整的:


  1. 更新.TT文件为POCO一代,使实体类部分。这将使您能够在一个单独的文件中提供附加的实现。

  2. 请部分实施用户的类在另一个文件

 

 部分用户:IUSER
{
}

这将使用户类实现右侧接口,不接触实际生成的文件(编辑生成的文件始终是一个糟糕的主意)。

Is it possible to use the new Asp.net Identity with Database First and EDMX? Or only with code first?

Here's what I did:

1) I made a new MVC5 Project and had the new Identity create the new User and Roles tables in my database.

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) 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.

I guess a possible solution is to edit my POCO Generation Classes to have my User class inherit from IUser?

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) 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) 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) 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; }

解决方案

It should be possible to use the identity system with POCO and Database First, but you'll have to make a couple of tweaks:

  1. 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.
  2. Make a partial implementation of the User class in another file

 

partial User : IUser
{
}

That will make the User class implement the right interface, without touching the actual generated files (editing generated files is always a bad idea).

这篇关于ASP.NET身份与EF数据库优先MVC5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆