I can’t figure out why this is causing EF error: Invalid column name ‘User_UserId’ when saving in EF.
Here is my model:
[DataContract(IsReference = true)]
public class User
{
[Key]
[DataMember]
public virtual Guid UserId { get; set; }
[DataMember]
public virtual string Username { get; set; }
[DataMember]
public virtual string Password { get; set; }
[DataMember]
public virtual string Email { get; set; }
[DataMember]
public virtual ICollection<FriendList> FriendLists { get; set; }
}
[DataContract(IsReference = true)]
public class FriendList
{
[Key]
[DataMember]
public virtual Guid FriendListId { get; set; }
[DataMember]
[ForeignKey("User")]
public virtual Guid UserId { get; set; }
[DataMember]
public virtual User User { get; set; }
[DataMember]
[ForeignKey("FriendUser")]
public virtual Guid FriendUserId { get; set; }
[DataMember]
public virtual User FriendUser { get; set; }
}
basically, its one to many relationship with users having a friendlists.
You have two navigation properties of type
Userin yourFriendListclass. EF cannot figure out which of these belong toUser.FriendListsand then creates for all three navigation properties a separate one-to-many relationship, one of them has the default foreign key nameUser_UserId.You can overwrite this convention with the
InversePropertyattribute:Now,
User.FriendListsandFriendList.Userare the endpoints of the same one-to-many relationship andFriendList.FriendUserdefines a second one-to-many relationship (but without an endpoint in theUserclass).