Print Page | Close Window

import Entities with EntityState.Added into a sandbox

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2574
Printed Date: 31-May-2025 at 8:24am


Topic: import Entities with EntityState.Added into a sandbox
Posted By: bebe
Subject: import Entities with EntityState.Added into a sandbox
Date Posted: 22-Mar-2011 at 3:12am

I'd like to show the user the consequences of his/her editing before applying it to the main context. So I import the entites in a seperate editing context and ask for confirmation. I got some problems when the entities are just created: they get a new ID in the sandbox.

Is it stupid to put newly created entities in a sandbox? Or am I doing something wrong (see  code below)

                       //create some new Entities 'SubjectForm'

            var sf1 = SubjectForm.Add(...);
            var sf2 = SubjectForm.Add(...);
            var sf3 = SubjectForm.Add(...);

            var roots = new[] { sf3 };

            var spans = new[] {
                new EntitySpan(SubjectForm.PropertyMetadata.Form.RelationLink),
                new EntitySpan(SubjectForm.PropertyMetadata.Subject.RelationLink)               
            };

            var graph = _manager.FindEntityGraph(roots, spans, EntityState.AllButDetached);
            /*
            graph
            Count = 3
                [0]: {SubjectForm: -102} - Added
                [1]: {Form: 1000004} - Unchanged
                [2]: {Subject: 1000001} - Unchanged
            */
                        
            var sandbox = new MyEntityManager();
            sandbox.ImportEntities(graph, MergeStrategy.OverwriteChanges);

            /*
            sandbox.FindEntities(EntityState.AllButDetached).OfType<object>().ToList()            
            Count = 3
                [0]: {SubjectForm: -100} - Added
                [1]: {Form: 1000004} - Unchanged
                [2]: {Subject: 1000001} - Unchanged
            */

            var clone = sandbox.FindEntity(sf3.EntityAspect.EntityKey);
            Assert.IsNotNull(clone); //
SubjectForm got a new ID and can not be found




Replies:
Posted By: sbelini
Date Posted: 22-Mar-2011 at 4:39pm
Hi bebe,
 
When you import entities into an EntityManager, all the temporary IDs are reset. (after all, they are temporary. Also, there is a possibility that the EntityManager could have this temporary ID already associated with another entity)
 
Is this situation, you should resolve the IDs before importing them into the sandbox.
 
i.e.
_manager.ForceIdFixup();
 
Also, I assume you would edit and/or save the entities in the sandbox and then import then back to your 'main' EntityManager. In this situation resolving the IDs is also important because when you import the entities back to the 'main' EntityManger, you will not have 'duplicate' entities (i.e. one with the resolved ID and EntityState.Unchanged and another with the temp ID and EntityState.Added)
 
 
   Silvio.



Print Page | Close Window