How to update navigation properties / linked tables in EF?

I have a Customer object with the Navigation property Days (days is a separate table that has day_id, customer_id is FK).

 mycontext.Customers.ApplyCurrentValues(cust); mycontext.SaveChanges(); 

This updates the scalar properties of the Client, not the days. Is there any smart way to update Days? (without repeating manually by day ..)? if not - is there any best practice for updating the second table (days)? If possible, write that explicit code should be used.

ps I am currently using EF 4.0

+4
source share
2 answers

No, you cannot do this without manually repeating for related objects. Take a look at this question and answer that may be helpful. MVC Entity Framework Modifying Child Objects

+1
source

You can use the GraphDiff library if you want a simple and easy way to work with a disabled object.

 mycontext.UpdateGraph(cust, map => map.OwnedCollection(x => x.Days)); mycontext.SaveChanges(); 

This will add or update the client object and also update the Days collection.

+4
source

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


All Articles