This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
ID's of new objects not being set to a negative value when new items are added to the collection. this is all performed in Memory, without any save to the DB. Clone a tree of objects eg: a Country contains Citys which contains a collection of family's, and family's contain a list of people. When cloning a city and all the child objects (and then add the new city to the country) Perform this action a few times. Then Delete one of the cities. The next clone you perform will have objects with an ID of "0" where pervious clones had the correct negative ID assigned to them. The actual code is far more complex, and not suitable for a demo here to illistrate the point.
regards Anthony
|
|
|
Could you provide a bit more detail on how you are cloning the object tree? Also, which IdentityMethod are you using? Thanks! |
|
|
I'm using AutoIncrement as an Identity. I can send you a sample, but if would be more than 10 tables in an hierarchy, and the cloning structure is complex, id rather not as it would add unnecessary complexity, and deter from the real problem, but if you can't re-produce the problem, let me know and Ill send the DB and code.
Anthony |
|
|
Hi Anthony, We don't need the complex repro code, but we do need some idea of how you are doing the cloning -- e.g. MemberwiseClone, manual copying of properties, deep copy when cloning associations, etc. Just a quick verbal description, no need for sample code. Thanks! |
|
|
Hi, I clone all fields in an object, then run through it's child items, and clone them. In some instances where a child has 2 parent objects I run down the one tree, cloning the child items, and in the other tree I just add the created object (from the other tree) to this objects child collection. This precedure works perfectly, but when performing the cloning serveal times, and removing one of the cloned items, then cloning more items, the newly cloned items have an id of "0" instead of "-xxx". I'm using Identity method for identities. Thx, Anthony
/// <summary> |
|
|
Thanks Anthony -- I have now managed to reproduce this (so hopefully won't need to bug you any more!) and will look into the problem. |
|
|
Hi Anthony, It turns out this is not down to cloning but to an issue with the removal logic of new entities (that is, entities in the New state). Basically the Remove is causing the parent (Country) to become confused, so that subsequent Adds no longer result in the added child being implicitly brought into the parent's unit of work. Since LightSpeed doesn't allocate IDs to objects that are not part of a UOW, you are seeing the IDs remain at 0 (and, more worryingly, if you were to save the parent, the children added after the remove would not get saved). We are looking into a fix for this but there are a lot of potential subtleties so in the meantime I would suggest one of two workarounds: * Use a short-lived unit of work: that is, add the parent (Country) to the UOW only after you have done all the adding and removing that is required, probably just before you save. The impact of this is that you will not have IDs for *any* of your entities until you do that delayed add. I am not sure what you are using the temporary IDs for so I don't know if this will be an issue for you. * Explicitly add the Cities to the unit of work -- i.e. when you add a cloned City to the Country.Cities collection, also call UnitOfWork.Add(clonedCity). Normally we do this implicitly for you but as I mentioned this implicit Add stops working after a Remove; but you can still do it explicitly and you will then find that the negative ID is available to you again (and, more importantly, the entity gets saved correctly!). Note that when you explicitly add a City to the UOW, that will automatically propagate to its children (Families I think you said), so you do not need to do this recursively; however, if you subsequently remove Families from that City and then want to add more, then you will need to use the same workaround on the newly added Families. I hope one of these two workarounds is viable for you; if not then let us know and we will see if we can think of something else while we try to address the underlying issue. |
|
|
Thx, ill use the workaround in the meantime. But im looking forward to the fix
Anthony |
|