I have two objects: User and Relationship. A User object is created for each user of my system and multiple relationships will be created for each user depending on how many people they add as friends. The objects look as follows
public class User : IdentityUser {}
public class Relationship {
// User who created the relationship
public User User {get;set;}
// User who the creation user is friends with
public User Friend {get;set;}
// Enum of Status (Approved, Pending, Denied, Blocked)
public RelationshipStatus RelationshipStatus {get;set;}
public DateTime RelationshipCreationDate {get;set;}
}
Each user could have multiple records where they match Relationship.User
and they could have multiple records where they match Relationship.Friend
. I have my DbContext setup as follows in the OnModelCreating
method:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Relationship>(relationship =>
{
relationship.HasOne(r => r.User).WithMany(u => u.Friends);
});
}
I'm new to Entity so this is probably trivial - What am I missing to have the relationship setup as I have described? Thanks!
Edit: To Be more specific, here is the error that I'm getting:
InvalidOperationException: Unable to determine the relationship represented by navigation property 'Relationship.Friend' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
Edit 2: After rethinking my process, I took a different approach and used the relationships to include followers and following, rather than a collective friends list (fits my business model better anyway). I achieved this with the following:
public class User : IdentityUser {
[InverseProperty("Friend")]
public List<Relationship> Followers {get;set;}
[InverseProperty("User")]
public List<Relationship> Following {get;set;}
}
I am curious if anyone has a solution to the original problem outside of my solution, however, this works for now and seems to fit me best.
Have you tried adding to the User class a List of Relationships, i.e
public class User : IdentityUser {
List<Relationship> Relationships {get;set;}
}
This might help EF fully understand the relationship.