5

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

AstroCB
  • 12,337
  • 20
  • 57
  • 73
BornToCode
  • 9,495
  • 9
  • 66
  • 83

2 Answers2

5

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.

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
  • 1
    just to point out that for this to work you will need to set `myContext.Configuration.AutoDetectChangesEnabled` to true (if it is disabled). – fabriciorissetto May 27 '16 at 22:47
1

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

Community
  • 1
  • 1
Bishnu Paudel
  • 2,083
  • 1
  • 21
  • 39
  • I got the impression that it suppose to be possible from this [post](http://stackoverflow.com/questions/9606866/cannot-get-relationship-to-update-for-navigation-properties-in-entity-framework), but couldn't implement it, does that post not relevant to the situation I describe? – BornToCode Sep 12 '12 at 01:38
  • 2
    @BornToCode: Yes, the post linked in your comment is not relevant because you have a navigation *collection* to update, the other post is about updating a navigation *reference*. An approach to update collections is this one: http://stackoverflow.com/a/5540956/270591 (It's for `DbContext`/EF>=4.1 though, but the idea and the code should be translatable to `ObjectContext`/EF 4.0.) – Slauma Sep 12 '12 at 19:33
  • Thank you for your patience with my questions, I appreciate it. – BornToCode Sep 13 '12 at 15:37