New Posts New Posts RSS Feed: Strange behaviror : duplicated entities when using sand box editors
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Strange behaviror : duplicated entities when using sand box editors

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

Joined: 14-Jul-2013
Location: Algeria
Posts: 1
Post Options Post Options   Quote chk040399 Quote  Post ReplyReply Direct Link To This Post Topic: Strange behaviror : duplicated entities when using sand box editors
    Posted: 29-Oct-2013 at 1:32am
Hi !,
I have a strange behavior,
I 'm using sandbox editors to edit/add an entity,
I have a main entity manager, and an entity manager used to edit/add entities
When I create new entity, (using the sandbox EM), and import it to the main EM, there is no problem.
then I try to edit the newly created entity (which is not commited to Database) , in a new sandBox EM, and import it again
in the main EM, there is 2 new entities !!!

see the code below (I used linqpad, My model is named GeDoseEntities)

void Main()
{

    var em1 = new GedoseEntities(); //main Entity Manager
   
   
   
    // 1st editor
    var em2 = new GedoseEntities(); //this EM will create the entity
    var newsite = new Site() { Nom="service d", OrganismeId = 1};
    em2.AddEntity(newsite);
   
    //1st edition : creation
    bool validateCreation = true;
   
    if(validateCreation)
    {
        var entities = em2.FindEntities(IdeaBlade.EntityModel.EntityState.AnyAddedModifiedOrDeleted);
        em1.ImportEntities(entities,MergeStrategy.OverwriteChanges);   
   
    }
    else
    {
        em2.Clear();
        return;
    }
   
    // at this point the newly crated entity is imported to the main EM , and there is only one instance
   
    // dump into linqpad outut
    em1.FindEntities(IdeaBlade.EntityModel.EntityState.AnyAddedModifiedOrDeleted).Dump("after creation");   
   
   
    // edit the newlyt created entity (!! : not commited to the database)
    int id = newsite.Id;   
    var siteToEdit = em1.Sites.FirstOrDefault(s=>s.Id==id);
   
    // EM used to edit the entity
    var em3 = new GedoseEntities();       
    var entitiesToimport = em1.FindEntities(IdeaBlade.EntityModel.EntityState.AnyAddedModifiedOrDeleted);
    em3.ImportEntities(entitiesToimport,MergeStrategy.OverwriteChanges);   
    em3.FindEntities(IdeaBlade.EntityModel.EntityState.AllButDetached).Dump("e3");   
   
    {
        var siteToModify = em3.Sites.FirstOrDefault(s=>s.Id==id);   
   
        // modif
        siteToModify.Nom += "+++";   
        siteToEdit.Dump("site to edit");
        siteToModify.Dump("site to modify");   
    }
   
   
    bool validateModification = true;
   
    if(validateModification)
    {       
        var entities = em3.FindEntities(IdeaBlade.EntityModel.EntityState.AnyAddedModifiedOrDeleted);           
        em1.ImportEntities(entities,MergeStrategy.OverwriteChanges);   
    }
    else
    {
        em3.Clear();
    }
   
    em1.FindEntities(IdeaBlade.EntityModel.EntityState.AnyAddedModifiedOrDeleted).Dump("To update");
    // at this point there is two new entities !!!
   
}


Thanks




Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 29-Oct-2013 at 9:59am
This strange behavior occurs because you're using temporary ids.  While temporary ids can be useful they also have drawbacks, most specifically in that they don't provide a true fixed identity for an entity.   When importing, DevForce doesn't know that the Site entities in the two EMs are truly the same, and creates a new Site entity.  You can work around this behavior by assigning real ids in your code, such as Guids, rather than having the database assign a real id when the item is saved.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down