New Posts New Posts RSS Feed: import Entities with EntityState.Added into a sandbox
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

import Entities with EntityState.Added into a sandbox

 Post Reply Post Reply
Author
bebe View Drop Down
Newbie
Newbie
Avatar

Joined: 11-Feb-2010
Location: Austria
Posts: 6
Post Options Post Options   Quote bebe Quote  Post ReplyReply Direct Link To This Post Topic: import Entities with EntityState.Added into a sandbox
    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

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down