【问题标题】:The property 'Claims' on type 'AspNetUser' is not a navigation property“AspNetUser”类型的属性“声明”不是导航属性
【发布时间】:2015-07-07 01:43:12
【问题描述】:

我正在使用 ASP.NET Identity 2.2。我正在将 ASP.NET 旧成员身份迁移到新身份系统。我正在按照this article 中提到的步骤执行迁移。

我扩展了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; }

}

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 标识表的数据库图:

谁能帮我解决这个问题?非常感谢任何帮助。

【问题讨论】:

  • IdentityUser 已经有Claims 属性时,为什么你的模型中还需要AspNetUserClaims 属性?
  • @DavidG:感谢您的快速回复。我已将新的错误列表添加到问题中。如果我从 IdentityUser 中删除 AspNetUserClaims 属性,我会收到这些新错误

标签: c# entity-framework asp.net-identity claims-based-identity usermanager


【解决方案1】:

您可以在此处查看 IdentityUser 的属性:https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identityuser_properties(v=vs.108).aspx

如您所见,Claims、Logins、Roles 等属性已经存在。 默认情况下,asp.net 身份使用 DbContext,它继承自 IdentityDbContext https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identitydbcontext%28v=vs.108%29.aspx

这个类配置了很多东西,比如表映射等。我们能看到你的 DbContext 吗?

首先,尝试从构造函数中删除添加的 ICollections 及其初始化器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多