“AspNetUser”类型上的“声明”属性不是导航属性 [英] The property 'Claims' on type 'AspNetUser' is not a navigation property
问题描述
我已经扩展了 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;组; }
public virtual ICollection< AspNetUserClaim> AspNetUserClaims {get;组; }
public virtual ICollection< AspNetUserLogin> AspNetUserLogins {get;组; }
public virtual ICollection< AspNetRole> AspNetRoles {get;组; }
}
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);
错误是:
AspNetUser类型上的声明属性不是导航属性。
参考和集合方法只能与导航属性一起使用。使用Property或ComplexProperty方法。
更新:
从 AspNetUser
类中删除 AspNetUserClaims
属性,然后我得到一个新的错误列表:
指定的模式无效。错误:
由于MyEntities.AspNetUser类型不可用,因此未加载关系JanEntities.FK__AspNetU__Appli__628FA481。
以下信息可能有助于解决以前的错误:
所需的属性AspNetUserClaims不存在于SampleApp.Core.AspNetUser类型上。
由于MyEntities.AspNetUser类型不可用,因此未加载MyEntities.AspNetUserRole关系。
以下信息可能有助于解决以前的错误:
所需的属性AspNetUserClaims不存在于SampleApp.Core.AspNetUser类型上。
由于MyEntities.AspNetUser类型不可用,因此未加载关系MyEntities.FK_dbo_AspNetUserClaim_dbo_AspNetUser_User_Id。
以下信息可能有助于解决以前的错误:
所需的属性AspNetUserClaims不存在于SampleApp.Core.AspNetUser类型上。
MyEntities.FK_dbo_AspNetUserLogin_dbo_AspNetUser_UserId关系未加载,因为MyEntities.AspNetUser类型不可用。
以下信息可能有助于解决以前的错误:
所需的属性AspNetUserClaims不存在于SampleApp.Core.AspNetUser类型上。
以下是包含新ASP.NET身份表的数据库图:
< img src =https://i.stack.imgur.com/FLNKq.pngalt =在此输入图像说明>
任何人都可以帮我修复这个问题?任何帮助是非常感谢。
您可以在这里查看IdentityUser的属性: https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity .entityframework.identityuser_properties(v = vs108).aspx
如您所见,声明,登录,角色等属性已经存在。
默认情况下,asp.net身份正在使用DbContext,它继承自IdentityDbContext https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identitydbcontext%28v=vs.108%29.aspx
这个类配置很多东西,如表映射等。我们可以看到你的DbContext吗?
首先,尝试从构造函数中删除添加的ICollections及其初始化器。
I'm using ASP.NET Identity 2.2. I'm migrating the ASP.NET old membership to new Identity system. I am following the steps mentioned in this article for performing the migration.
I have extended IdentityUser
and added few more properties like follows:
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; }
}
There are few more properties in the AspNetUser
class which is not included for brevity.
I am able to register the user successfully using the identity system:
var manager = new ApplicationUserManager();
var user = new AspNetUser
{
UserName = UserName.Text.Trim(),
Email = Email.Text.Trim()
};
var result = manager.Create(user, Password.Text);
But when I'm searching for any user by email address/username then I'm getting an exception:
var existingUser = manager.FindByEmail(emailAddress);
The error is:
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.
Update:
If I'm removing the AspNetUserClaims
property from the AspNetUser
class then I'm getting a list of new errors:
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'.
The following is the database diagram which contains the new ASP.NET identity tables:
Can anyone help me to fix this issue? Any help is highly appreciated.
You can check here properties of IdentityUser: https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identityuser_properties(v=vs.108).aspx
As you can see, properties like Claims, Logins, Roles are already there. By default asp.net identity is using DbContext, which inherits from IdentityDbContext https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identitydbcontext%28v=vs.108%29.aspx
This class configure many things like table mappings etc. Can we see your DbContext?
So first, try removing added ICollections and theirs initializers from constructor.
这篇关于“AspNetUser”类型上的“声明”属性不是导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!