0

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?

jlavallet
  • 1,267
  • 1
  • 12
  • 33
  • If you prefer to use `Guid` you can always change the Id type. I answered a [couple](http://stackoverflow.com/a/30643391/219406) of [questions](http://stackoverflow.com/a/24764152/219406) which might help you. – LeftyX Sep 17 '15 at 08:23

1 Answers1

0

Okay, I figured out what was going wrong. I was using a nullable Guid (Guid?) as the type of my ManagerId. Actually in the ASP.NET Identity framework, the prebuilt Entity Framework IdentityUser class uses a string type for its Id property. This string property is set to the value of a new Guid converted to a string using .ToString(). Once I figured that out (and since I know that string is nullable) I simply changed the type of my ManagerId property to string and everything worked. So my problem was addressed by figuring out the right type in the Identity framework, not by annotating the property a different way for the Entity Framework. I am curious if anyone could answer the original question if the Id was not a nullable type.

jlavallet
  • 1,267
  • 1
  • 12
  • 33