Up Create, modify and delete

Import entities

Last modified on August 15, 2012 17:20

Occasionally, you may have a secondary workflow or editing context that needs to be isolated from the primary workflow. Creating another EntityManager and importing entities is a great way to create a "sandbox" environment that copies and isolates changes to the entities until you are ready to merge them back into the primary workflow.

The secondary EntityManager likely needs to be seeded with entities from the primary EntityManager. If they have already been cached, this avoids having to go back to the datasource to retrieve them, but more importantly, some of the entities may be modified and you want their modified state to be copied into the secondary manager.

Import entities into an EntityManager

To import entities into another EntityManager call ImportEntities.

C#
manager.ImportEntities(entities, MergeStrategy.PreserveChanges);
VB
manager.ImportEntities(entities, MergeStrategy.PreserveChanges)

This creates a copy of the entities into the cache of the EntityManager. Any changes to the copied entities in this cache will not affect the source entities. Notice that the second parameter of ImportEntities takes a MergeStrategy. This MergeStrategy determines how the entities are merged if they already exist in the target EntityManager's cache.

If you are importing entities into an empty EntityManager, then you don't need to worry about merge conflicts. This only occurs when you are merging into an EntityManager which may have potentially modified versions of the same entities. For an in depth discussion about merging entities see Merge query results into the entity cache.

Work with multiple EntityManagers

Remember that when working with multiple EntityManagers that each one is a separate editing context and that you may (and probably will) have multiple copies of the same entities (entities that have the same ID or primary key).

When you are binding the entities to the UI, make sure you understand which instance of the entity you are binding to. Be very clear about which EntityManager you are using to initialize a view, and avoid mixing entities from different EntityManagers in the same view.

To learn more about working with multiple EntityManagers see Multiple EntityManagers.

Find entities to import

If you don't have an easy reference to all the entities you want to import, FindEntityGraph can be of great assistance. FindEntityGraph starts with a set of root entities and retrieves all related entities for the specified associations (EntitySpans ).

FindEntityGraph does not query the datasource, so make sure that you already have the entities in cache by using the Include operator in the original query.
C#
var rootEntities = new List<Entity>();
rootEntities.Add(someEmployees);

var spans = new List<EntitySpan>();

EntitySpan span = new EntitySpan(typeof(Employee),
  EntityRelations.FK_Order_Employee,
  EntityRelations.FK_OrderDetail_Order,
  EntityRelations.FK_OrderDetail_Product);

spans.Add(span);

var entityGraph = manager.FindEntityGraph(rootEntities, spans, EntityState.AllButDetached);
VB
Dim rootEntities = New List(Of Entity)()
rootEntities.Add(someEmployees)

Dim spans = New List(Of EntitySpan)()

Dim span As New EntitySpan(GetType(Employee), _
  EntityRelations.FK_Order_Employee, _
  EntityRelations.FK_OrderDetail_Order, _
  EntityRelations.FK_OrderDetail_Product)

spans.Add(span)

Dim entityGraph = manager.FindEntityGraph(rootEntities, spans, EntityState.AllButDetached)

Temporary id remapping

During an import, any entities with a temporary id will be assigned a new temporary id to avoid conflicts with temporary ids that already exist in the cache. All foreign key references will also be remapped, so that object navigation will continue to work. Generally, you do not need to worry about this, but if you are inspecting the temp ids, this is why they change after import.

Tags:
Created by DevForce on February 17, 2011 18:22

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade