ASP.NET Identity開發人員應該識別出我的重命名的ApplicationUser類(現稱爲User),該類是從具有基於Guid的Id屬性的IdentityUser派生而來的。在我的User類中,我有一個可選的自引用外鍵(public 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'.
我知道有可空Guid類型的經理ID做。那麼我該怎麼做?
如果您更喜歡使用'Guid',則可以隨時更改Id類型。我回答了[問題](http://stackoverflow.com/a/24764152/219406)[夫婦](http://stackoverflow.com/a/30643391/219406),它可能會幫助你。 – LeftyX