2

This works as expected:

var customer = getCustomerFromSomewhere();
customer.Name = "Foo";
context.SaveChanges();
// ...
// ... in an override of SaveChanges, get the entry state
var isModified = entryState == EntityState.Modified;  // ==true

The simple property was changed, so the entity is automatically marked as modified by the context.

Now consider a change to a navigation property which is a collection:

var customer = getCustomerFromSomewhere();
customer.Orders.Clear();
var order = getOrderFromSomewhere();
customer.Orders.Add(order);
context.SaveChanges();
// ...
// ... in an override of SaveChanges, get the entry state
var isModified = entryState == EntityState.Modified;  // ==false

So the entity is not automatically marked as modified in this case. I get around this by manually marking it as modified, but I want it to occur automatically.

How do I do that? How do I detect whether an entity's relationships are modified?

h bob
  • 3,610
  • 3
  • 35
  • 51

1 Answers1

1

The trick is to use the answer here, but that didn't work for me until I implicitly forced checking for changes:

myContext.ChangeTracker.DetectChanges();
h bob
  • 3,610
  • 3
  • 35
  • 51