类型为'AspNetUser'的属性'Claims'不是导航属性。

12

我正在使用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; }

}
< p >为了简洁起见,AspNetUser类中还有一些属性未包含在内。

我能够使用身份验证系统成功地注册用户:

 var manager = new ApplicationUserManager();
 var user = new AspNetUser
                {
                    UserName = UserName.Text.Trim(),
                    Email = Email.Text.Trim()
                };

 var result = manager.Create(user, Password.Text);

但是,当我通过电子邮件地址/用户名搜索任何用户时,我会收到一个异常:

var existingUser = manager.FindByEmail(emailAddress);

错误信息为:

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.

更新:

如果我从AspNetUser类中移除AspNetUserClaims属性,那么就会出现一系列新错误:

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'.
以下是包含新的 ASP.NET 身份验证表的数据库图表: enter image description here 有人可以帮我解决这个问题吗?非常感谢任何帮助。

为什么在你的模型中需要AspNetUserClaims属性,而IdentityUser已经有了一个Claims属性? - DavidG
@DavidG:感谢您的快速回复。我已经将新的错误列表添加到问题中。如果我从“IdentityUser”中删除“AspNetUserClaims”属性,我会得到这些新的错误。 - Dukhabandhu Sahoo
1个回答

1
您可以在这里查看IdentityUser的属性: https://msdn.microsoft.com/zh-cn/library/microsoft.aspnet.identity.entityframework.identityuser_properties(v=vs.108).aspx 正如您所见,像Claims、Logins、Roles之类的属性已经存在。默认情况下,asp.net identity使用DbContext,该类继承自IdentityDbContexthttps://msdn.microsoft.com/zh-cn/library/microsoft.aspnet.identity.entityframework.identitydbcontext(v=vs.108).aspx 这个类配置了许多东西,比如表映射等。我们可以看一下您的DbContext吗?
所以首先,请尝试从构造函数中删除添加的ICollections及其初始化器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接