Hi Arthur
Using Add method will add all objects in graph of objects but as you said, it will not update existing entities(if they have same keys as one of the entities in the graph that is being added). In EF 4.0 this was throwing exception on SaveChanges because EF was trying to insert already existing entity, even after calling DetectChanges.
I think my method supports both scenarios you mentioned. It should make sure that any combination of entities in graph, whether existing or not, is persisted to DB such as provided to this method (saving scalar properties, and related entities, although this can be modified by adding deleting information to insert/save method)
Maybe I can explain functionality of my code better.
{In my case we have layered structure of application, so data access layer will send data to user interface and when changes are done, they are sent to DAL and saved in DB.} My code is doing the following: you will call a method where inserted/updated entity(call it entityToSave) is supplied as method parameter(with rest of the graph connected via related ends). This insert/update method checks whether this entityToSave exists in context (by checking entity key). If such entity exists(lets call this entity entityInContext) it will call ApplyCurrentValues on entityToSave. Then it goes through all the related ends of entityToSave recursively. If entity is already in context it will reconect related end to entityInContext, if there are new or different entities in related ends it will change related ends so that they contain same data as entityToSave (although this can be modified by supplying insert/update method with more parameters which contain information which related ends to remove or not, if realted entity is not in entityToSave related ends).
for correct functionality, its neccesary to load entities that can be included in such a graph...
Example Model:
![Image]()
Example 1:
We want to save graph from model(ABC), where none of the saved entities are in db or context.
In this case method will do only add for all entites
Example 2:
We want to save graph where entity C is already in the context/db. my method will go through the saved graph, finds out that C is already included.
It will call ApplyCurrentValues so it will scalar properties on entity C. It connects entity B which is new, to existing C entity in context. And then SaveChanges can be called.
Example 3:
We want to save graph where sone B entities are already in context/db, and some are not. My method goes through saved graph and finds out that some B entities exist in context, and calls ApplyChanges on existing B entities, then it connects realted ends in A, B, and C entities so they connect to existing B entites.
For entities that are new to context, add will be called.
For entities that are existing in context, but are not part of saved graph, action will be taken depending on supplied information for deleting not included entities.
Default is that entities are only added to context entities(saving same A entity with different B entities, will create A entity with all B entities from save 1 and save 2).
Deleting data can be provided for each related end in graph(although at the moment its not very user friendly to create it).
Hope this will clear the confusion :-)
Using Add method will add all objects in graph of objects but as you said, it will not update existing entities(if they have same keys as one of the entities in the graph that is being added). In EF 4.0 this was throwing exception on SaveChanges because EF was trying to insert already existing entity, even after calling DetectChanges.
I think my method supports both scenarios you mentioned. It should make sure that any combination of entities in graph, whether existing or not, is persisted to DB such as provided to this method (saving scalar properties, and related entities, although this can be modified by adding deleting information to insert/save method)
Maybe I can explain functionality of my code better.
{In my case we have layered structure of application, so data access layer will send data to user interface and when changes are done, they are sent to DAL and saved in DB.} My code is doing the following: you will call a method where inserted/updated entity(call it entityToSave) is supplied as method parameter(with rest of the graph connected via related ends). This insert/update method checks whether this entityToSave exists in context (by checking entity key). If such entity exists(lets call this entity entityInContext) it will call ApplyCurrentValues on entityToSave. Then it goes through all the related ends of entityToSave recursively. If entity is already in context it will reconect related end to entityInContext, if there are new or different entities in related ends it will change related ends so that they contain same data as entityToSave (although this can be modified by supplying insert/update method with more parameters which contain information which related ends to remove or not, if realted entity is not in entityToSave related ends).
for correct functionality, its neccesary to load entities that can be included in such a graph...
Example Model:

Example 1:
We want to save graph from model(ABC), where none of the saved entities are in db or context.
In this case method will do only add for all entites
Example 2:
We want to save graph where entity C is already in the context/db. my method will go through the saved graph, finds out that C is already included.
It will call ApplyCurrentValues so it will scalar properties on entity C. It connects entity B which is new, to existing C entity in context. And then SaveChanges can be called.
Example 3:
We want to save graph where sone B entities are already in context/db, and some are not. My method goes through saved graph and finds out that some B entities exist in context, and calls ApplyChanges on existing B entities, then it connects realted ends in A, B, and C entities so they connect to existing B entites.
For entities that are new to context, add will be called.
For entities that are existing in context, but are not part of saved graph, action will be taken depending on supplied information for deleting not included entities.
Default is that entities are only added to context entities(saving same A entity with different B entities, will create A entity with all B entities from save 1 and save 2).
Deleting data can be provided for each related end in graph(although at the moment its not very user friendly to create it).
Hope this will clear the confusion :-)