Entity Framework: One-to-many relationship update for a child collection of an object

View the app for houses and paintings. An entity with a name Househas a ratio of 1: n to HousePicture.

The application allows the user to create and edit House, as well as add / remove HousePictures.

[HttpPost]
public ActionMethod Edit(House h)
{
   //get an attached entity from the DB first.
   House original = db.Houses.SingleOrDefault(x=>x.ID==h.ID);

   UpdateModel(original);

   //collect all the uploaded pictures.
   original.HousePictures = GatherPicturesFromForm();


   db.SaveChanges();
   // the changes for the House are saved, 
   //but child collection Pictures are not.
}

How are you going to update - add new and delete - the child collection when you recreate the child collection from scratch?

  • Add()or Attach()for each child in the collection?
  • In what sequence do you need to add or bind a parent to a child collection?
  • , ? EF4, , ?
  • HousePicture == 0. SQL Server, - PK int IDENTITY(1,1). , EF4 , 2+ .

  • Entity Framework 4?

  • 1: n //?
+3
1

EF , .

original.HousePictures.AddRange(GatherPicturesFromForm());

0

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


All Articles