该物业的声明'在类型' AspNetUser'不是导航属性

时间:2015-04-28 09:26:39

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

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

更新

如果我从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'.

以下是包含新ASP.NET身份表的数据库图: enter image description here

任何人都可以帮我解决这个问题吗?任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以在此处查看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和他们的初始值设定项。