0

Imagine this graph:

Device 1..* OperatingEnvironment 1..* NetworkEndpoint

If I construct a detached entity graph in-memory and the Device at the root of the graph is new while the OperatingEnvironment exists and the NetworkEndpoint exists but is assigned to a different OperatingEnvironment, will EF figure all this out?

When I say 'figure this out', its simply a case of walking the graph and doing (IfExist ? MergeValues : AddNew) Because the entities all have foreign keys, relationships should effectively get reassigned automatically during the property merge.

Is this how EF works or will I have to walk my own graph and apply this logic? If I do this, then my in-memory graph will have to be weakly linked, i.e. avoid linking via navigation properties and instead use the foreign key values, otherwise the whole graph will be added as as soon as I add one entity since EF will crawl the navigation properties.

Thanks - hope that's clear as mud.

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
  • In the meantime, I've done the latter. Weakly linked graph, manually walked and updated/added using a special ApplyCurrentValues extension I wrote to maintain the CreatedDate stamps. – Luke Puplett Jun 25 '12 at 18:27

1 Answers1

1

You must do it yourselves. EF has no internal logic to detect if the entity exists in the database or not.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670