Duk*_*hoo 12 c# entity-framework claims-based-identity asp.net-identity usermanager
我正在使用ASP.NET Identity 2.2.我正在将ASP.NET旧成员资格迁移到新的Identity系统.我按照本文中提到的步骤执行迁移.
我已经扩展IdentityUser并添加了更多的属性,如下所示:
public partial class AspNetUser : IdentityUser
{
public AspNetUser()
{
CreateDate = DateTime.Now;
IsApproved = false;
LastLoginDate = DateTime.Now;
LastActivityDate = DateTime.Now;
LastPasswordChangedDate = DateTime.Now;
LastLockoutDate = DateTime.Parse("1/1/1754");
FailedPasswordAnswerAttemptWindowStart = DateTime.Parse("1/1/1754");
FailedPasswordAttemptWindowStart = DateTime.Parse("1/1/1754");
Discriminator = "AspNetUser";
LastModified = DateTime.Now;
this.AspNetUserClaims = new HashSet<AspNetUserClaim>();
this.AspNetUserLogins = new HashSet<AspNetUserLogin>();
this.AspNetRoles = new HashSet<AspNetRole>();
}
....
public virtual Application Application { get; set; }
public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
AspNetUser为了简洁起见,该类中没有更多属性.
我能够使用身份系统成功注册用户:
var manager = new ApplicationUserManager();
var user = new AspNetUser
{
UserName = UserName.Text.Trim(),
Email = Email.Text.Trim()
};
var result = manager.Create(user, Password.Text);
Run Code Online (Sandbox Code Playgroud)
但是,当我通过电子邮件地址/用户名搜索任何用户时,我得到一个例外:
var existingUser = manager.FindByEmail(emailAddress);
Run Code Online (Sandbox Code Playgroud)
错误是:
The property 'Claims' on type 'AspNetUser' is not a navigation property.
The Reference and Collection methods can only be used with navigation properties. Use the Property or ComplexProperty method.
Run Code Online (Sandbox Code Playgroud)
如果我AspNetUserClaims从AspNetUser类中删除该属性,那么我将获得一个新错误列表:
Schema specified is not valid. Errors:
The relationship 'JanEntities.FK__AspNetU__Appli__628FA481' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.
The relationship 'MyEntities.AspNetUserRole' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.
The relationship 'MyEntities.FK_dbo_AspNetUserClaim_dbo_AspNetUser_User_Id' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.
The relationship 'MyEntities.FK_dbo_AspNetUserLogin_dbo_AspNetUser_UserId' was not loaded because the type 'MyEntities.AspNetUser' is not available.
The following information may be useful in resolving the previous error:
The required property 'AspNetUserClaims' does not exist on the type 'SampleApp.Core.AspNetUser'.
Run Code Online (Sandbox Code Playgroud)
以下是包含新ASP.NET标识表的数据库图表:

任何人都可以帮我解决这个问题吗?任何帮助都非常感谢.
您可以在此处检查 IdentityUser 的属性:https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identityuser_properties (v=vs.108).aspx
如您所见,声明、登录、角色等属性已经存在。默认情况下,asp.net 身份使用 DbContext,它继承自 IdentityDbContext https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identitydbcontext%28v=vs.108%29.aspx
这个类配置了很多东西,比如表映射等。我们可以看看你的 DbContext 吗?
因此,首先,尝试从构造函数中删除添加的 ICollections 及其初始值设定项。
| 归档时间: |
|
| 查看次数: |
2363 次 |
| 最近记录: |