【发布时间】:2015-09-16 17:06:09
【问题描述】:
ASP.NET Identity 开发人员应该认识到我重命名的 ApplicationUser 类(现在称为 User)派生自 IdentityUser,它具有基于 Guid 的 Id 属性。在我的用户类中,我有一个可选的自引用外键(公共 Guid?ManagerId)和一个简单的匹配导航属性(公共用户管理器)。所有这些都有效。我的问题是我想要第二个导航属性(DirectlyManagedUsers),但我不知道如何对其进行注释,以使其包含该用户的直接管理用户的集合。我将不胜感激。
这是我的用户类:
public class User : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager, string authenticationType)
{
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
return userIdentity;
}
public User() : base()
{
DirectlyManagedUsers = new List<User>();
}
public User(string userName) : base(userName)
{
DirectlyManagedUsers = new List<User>();
}
[ForeignKey(nameof(Manager))]
public Guid? ManagerId { get; set; }
[ForeignKey(nameof(ManagerId))]
public User Manager { get; set; }
[InverseProperty(nameof(Manager))]
public ICollection<User> DirectlyManagedUsers { get; set; }
}
我在生成模型时遇到以下错误:
One or more validation errors were detected during model generation:
User_DirectlyManagedUsers_Source_User_DirectlyManagedUsers_Target: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ManagerId' on entity 'User' does not match the type of property 'Id' on entity 'User' in the referential constraint 'User_DirectlyManagedUsers'. The type of property 'ManagerId' on entity 'User' does not match the type of property 'Id' on entity 'User' in the referential constraint 'User_DirectlyManagedUsers'.
我知道这与 ManagerId 的可为空的 Guid 类型有关。那我该怎么办?
【问题讨论】:
标签: entity-framework asp.net-identity-2