Strange behaviror : duplicated entities when using sand box editors
Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4577
Printed Date: 06-Sep-2025 at 6:54am
Topic: Strange behaviror : duplicated entities when using sand box editors
Posted By: chk040399
Subject: Strange behaviror : duplicated entities when using sand box editors
Date 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
|
Replies:
Posted By: kimj
Date 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.
|
|