【发布时间】:2015-07-21 18:51:24
【问题描述】:
我目前在 ASP.NET 4.5 Web 表单项目中实现了现有成员资格。该应用程序使用带有DbContext 的EntityFramework 6.1.3 版本,目前采用数据库优先方法。我想迁移旧成员以使用新的 ASP.NET 身份 2.0 系统。
我已关注this article 执行迁移,但从未成功。我收到了The property 'Claims' on type 'AspNetUser' is not a navigation property 中提到的错误。
它确认我在迁移过程中遗漏了一些基本的东西。
谁能为我提供执行成功迁移的分步指南?
我想说明以下几点:
1) 在身份方面,除了默认身份提供的内容之外,我还有一些额外的用户数据。我看到有一个IdentityContext 执行这些身份操作。我有一个从DbContext 继承的不同上下文。我需要同时使用这两个上下文吗?换句话说IdentityContext是身份操作的强制要求?这两个上下文不能合二为一吗?
2) 我不限于使用数据库优先方法。由于身份使用代码优先方法,我可以继续使用代码优先方法,但需要正确的步骤来跟进。
3) 由于我有额外的用户数据,所以我需要扩展IdentityUser 以添加新属性?如果我将扩展IdentityUser,身份代码会无缝工作吗?
4) 我关注了https://www.youtube.com/watch?v=blmkPA7XQf8 视频教程,它在ApplicationDbContext 中添加了迁移。 ApplicationDbContext 继承 IdentityContext。它对我有用,但我想为我自己的继承自 DbContext 的上下文添加迁移。这是因为 ApplicationDbContext 不包含其他表(非标识表)。
5) 我尝试在包含所有身份和非身份表的自定义 Context.cs 文件中应用迁移。我按照this article 中的建议使用EntityFramework Power Tools 的逆向工程方法创建了这个类。
创建 POCO 类后,我添加了迁移并更新了数据库。我得到的主要错误是:
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
根据这个解决方案,我添加了配置,但最终为 IdentityUserLogin、IdentityRole 等创建了新表,这些表是重复的身份表。
6) 即使我保留了重复的身份表(例如 AspNetUserRoles 和 IdentityUserRole),我也无法使用身份代码获取数据
var user = userManager.FindAsync(UserName.Text, Password.Text); 并得到异常:
Invalid column name 'UserId'
【问题讨论】:
-
我明白了,这是一篇旧帖子,但是,你见过this tutorial 吗?也请看this post。
标签: c# asp.net entity-framework asp.net-membership asp.net-identity-2