How to update navigation properties / related tables in EF?
-
01-07-2021 - |
Вопрос
I have an object Customer
with a navigation property Days
(days is a separate table which have - day_id, customer_id - FK).
mycontext.Customers.ApplyCurrentValues(cust);
mycontext.SaveChanges();
This only updated the scalar properties of Customer, not days. Is There any smart way to update Days? (without iterating manually on days..)? if not - is there any best practice to update the second table (days)? If it's possible please write the explicit code should be used.
p.s. I'm currently using EF 4.0
Решение
No you can't do it without iterating manually on related entities. Have a look at this question and answer which might be helpful.MVC Entity Framework modifying child entities
Другие советы
You can use GraphDiff
library if you want a simple and easy way to work with disconnected object.
mycontext.UpdateGraph(cust, map => map.OwnedCollection(x => x.Days));
mycontext.SaveChanges();
This will insert or update customer object and also updating Days
collection as well.