jeudi 31 mai 2018

EF 6.2 - table with 2 navigation properties referencing to 1 table

I use EF 6.2 Database first. I have table dbo.Users and table dbo.Friends.

dbo.User:
ID (PK)
FirstName
LastName


dbo.Friends
FriendshipID (PK)
UserId
FriendID

both of UserID and FriendID are references as ForeignKey to dbo.User - ID. Then I have 2 virtual properties on Class Friends

public virtual User User_FriendID { get; set; }
public virtual User User_UserID { get; set; }

Selecting data and navigation properties works well. When I add a new record to table dbo.Friends

Friends fs = new Friends { UserId = 1, FriendId = 2};
entities.Friends.Add(fs);
entities.SaveChanges();

Code entities.Friends.Add(fs) creates a new record and fill only public virtual User User_UserID { get; set; } and that is the problem because I need both virtual properties filled.

proof of problem

Can anyone solve it?




Aucun commentaire:

Enregistrer un commentaire