Removing / adding a homing "friend" using Entity Framework 4.1 - DB First approach

I got the User model generated by ET 4.1. First I made a DB:

public partial class User { public User() { this.Photos = new HashSet<Photo>(); this.Posts = new HashSet<Post>(); this.My_requests = new HashSet<User>(); this.Requests = new HashSet<User>(); this.FriendLeft = new HashSet<User>(); this.FriendRight = new HashSet<User>(); } public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Email { get; set; } public string Project { get; set; } public string City { get; set; } public string Street { get; set; } public string House_number { get; set; } public string Apartment_number { get; set; } public string username { get; set; } public string Zip_code { get; set; } public virtual ICollection<Photo> Photos { get; set; } public virtual ICollection<Post> Posts { get; set; } public virtual ICollection<User> My_requests { get; set; } public virtual ICollection<User> Requests { get; set; } public virtual ICollection<User> FriendLeft { get; set; } public virtual ICollection<User> FriendRight { get; set; } } 

Each "User" can invite another "User" to become his friend. Therefore, when you are invited, he appears in the "Application" when you invite someone whom he appears in "My_requests". This data is stored in the table "Friend_requests", which looks like this:

ProposerId (col1) RecipientId (col2)

and both columns are dependent on the User table from UserId - from many to many.

My problem is to remove the row from "Friend_request", because EF does not give me context for this table, instead it generated the model shown earlier ...

I tried something like this:

 User User2 = GetUserInfo(UserId2); context.Users.Where(u => u.Id.Equals(UserId1)).SingleOrDefault().Requests.Remove(User2); context.SaveChanges(); 

But this, of course, is wrong, it gave me an error:

"It is not possible to update the EntitySet" Friends_Request "because it has a DefiningQuery element and the element does not have an element that can handle this operation."

And my questions are:
How can I delete and how to add rows to the table "Friend_Requests"?

EDIT:

Ratio table:
enter image description here

And in the model created by ET, it is:
My_requests โ†’ when the user offers Queries โ†’ when the User is the Recipient

The problem is how to add new โ€œfriend requestsโ€ and how to remove them?

+4
source share
1 answer

Of course, Slauma, as an experienced ET user, was right from the start. Without keys in the friends table, this will not work. Adding / removing items now works fine, as I write before. Juts are used as a regular list. Learn your own mistakes :)

0
source

Source: https://habr.com/ru/post/1436125/


All Articles