ASP.NET Identity developers should recognize my renamed ApplicationUser class (now called User) derived from IdentityUser which has a Guid-based Id property. In my User class I have an optional self referencing foreign key (public Guid? ManagerId) and one simple matching navigation property (public User Manager). All that works. My problem is I want a second navigation property (DirectlyManagedUsers) and I can't figure out how to annotate it such that it will contain a collection of this User's directly managed users. I'd appreciate some help.
Here is my User class:
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; }
}
I'm getting the following error on model generation:
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'.
I know that has to do with the nullable Guid type of the ManagerId. So what do I do?