Author |
Share Topic Topic Search Topic Options
|
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Topic: Possible Bug - Import entities Posted: 16-May-2011 at 11:38am |
Hi Derick,
I have prepared a sample. I have put the zip on our webserver:
http://71.164.192.3/ImportSL.zip
I have not used any frameworks with this sample, so it's not all best practices!
You will see two 'screens'
the top is the customer screen that lets you add/edit an address, when you click one of these buttons you will be able to update the address screen.
If you edit the address and save it, then save and edit again, you will see it starts duplicating the data. You will see it is not merging it in properly.
If you have any questions on it please let me know.
Thanks,
Mark
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 16-May-2011 at 12:02pm |
Thank you Mark. Appreciate the help. I'll get back in touch once I take a look at the sample.
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 26-May-2011 at 3:12pm |
Hi Mark;
Sorry it took so long to get back. Since you've already found a workaround, this has probably become a non issue for you.
Anyway, I just want to follow up what I found. It may or may not be useful.
The sample you sent does indicate that calling .SetModified() doesn't work. But the strange thing is that, when the EntityState is first Added, then SetModified(), the ImportEntities works. It will update the temp id from -100 to -101 and update the original CustomerAddress. However, when I modify the address again, ImportEntities stops working, i.e. it will keep the -101 CustomerAddress and create a new -102 CustomerAddress. What's even stranger is that I can't repro it outside the solution.
If I modify the code slightly to the following, using AcceptChanges(), then ImportEntities will keep the original temp id and not create duplicate CustomerAddress.
vm.EntityManager .FindEntities(EntityState.Added | EntityState.Modified) //.FindEntities(EntityState.Added) .OfType<Entity>() .ForEach(entity => entity.EntityAspect.AcceptChanges()); //.ForEach(entity => entity.EntityAspect.SetModified());
var entities = vm.EntityManager //.FindEntities(EntityState.AnyAddedModifiedOrDeleted) .FindEntities(EntityState.AllButDetached) .OfType<Entity>(); _primaryEntityManager.ImportEntities(entities, MergeStrategy.OverwriteChanges);
|
I'll continue to investigate this and provide updates when I've found something useful. But, again, as you've found a workaround, I think this issue can be considered closed.
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 26-May-2011 at 3:14pm |
Hi Denis,
Thanks for the update. I'm not sure if my workaround works in all cases, but I will retest it again when I get chance.
The only problem with AcceptChanges(), is that now they are unmodified, so a call to SaveChanges() won't pick those entities up will it?
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 26-May-2011 at 3:36pm |
Yes, you're correct. SaveChanges will not pick those entities up when you use AcceptChanges. So in the broader scenario, SetModified is probably preferable. All the more reason for me to figure out why I can't repro the above issue outside the solution. It should be as simple as importing the same modified entity again but it doesn't generate duplicate in my test project when I do that.
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 26-May-2011 at 7:13pm |
I can try and come up with a console based example that reproduces it. This was originally happening in another project and I made this to reproduce it.
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 27-May-2011 at 12:02pm |
I appreciate the help Mark. Thanks!
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 31-Oct-2011 at 3:17pm |
Hey Denis, I'm back on this project again and need some help. I have it "working" by doing this: Calling EntityAspect.AcceptChanges() then EntityAspect.SetModified() Then in the server save interceptor protected override bool ExecuteSave()
{
var entities = this.EntityManager
.FindEntities(EntityState.Modified)
.OfType<Entity>()
.ToList();
foreach (var entity in entities)
{
var key = (int) entity.EntityAspect.EntityKey.Values[0];
if (key < 0)
entity.EntityAspect.SetAdded();
}
return base.ExecuteSave();
}
If I take out the accept/set modified code, when I merge the additions from one EM into another it duplicates.
Has there been any progress on resolving these issues?
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 01-Nov-2011 at 4:52pm |
This is what I've had to do to make things work. It's not pretty, but it works. When I load a sandboxed editor up I merge the data in like this: OriginalManager.FindEntities<Entity>(EntityState.Added)
.ForEach(e =>
{
e.EntityAspect.AcceptChanges();
e.EntityAspect.SetModified();
});
var restoreStrategy = new RestoreStrategy(false, false, MergeStrategy.OverwriteChanges);
var data = repositoryBase.Manager.CacheStateManager.GetCacheState();
NewManager.CacheStateManager.RestoreCacheState(data, restoreStrategy);
var entities = Manager.FindEntities<Entity>(EntityState.Modified)
.Where(e => (int)e.EntityAspect.EntityKey.Values[0] < 0).ToList();
entities.ForEach(e => e.EntityAspect.SetAdded());
To stop the new manager from assigning a new Id, I have to call AcceptChanges and then SetModified.
Since I am using integer primary keys, I can find the true "Added" entities by finding the ones with a negative key. I then set these back to Added in the new manager. Also, to get around duplicating the same negative key, I have had to alter the default ID generator. It now uses a static _nextId variable so that all instances use the same id counter.
This seems to work, the only issue is on saving where even though they are set to added, they get duplicated in the EM after saving, the original Added entities, and the new saved unchanged entities.
I have had to do this to get around it:
private void OnSaveSuccess()
{
var addedEntities = Manager.FindEntities(EntityState.Added)
.OfType<Entity>()
.ToList();
for (var i = addedEntities.Count-1; i >= 0; i--)
{
addedEntities.EntityAspect.Delete();
}
}
If there is a better/cleaner way of doing this, I'm all ears.
Edited by smi-mark - 01-Nov-2011 at 5:00pm
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 02-Nov-2011 at 1:21pm |
Hi smi-mark;
It's been a while since we last discussed on this issue. I've read the whole posts again so let me try to summarize the issue(s).
I think we're dealing with 2 different issues here.
1. Best practices of using sandbox editor with 2 EntityManagers via ImportEntities.
As I recall, it is by design that if you have a new "Added" entity with a negative temp id and you're importing it to a second EM and importing it back to the original EM, it will create a duplicate entity because an "Added" entity with a negative temp id is always considered a new entity. The workaround is to set this entity to a "Modified" state before importing it back to the original EM.
The workaround that you show above seems pretty reasonable except for one thing. Why did you have to set entities.ForEach(e => e.EntityAspect.SetAdded()); after importing it back to the original EM? If you leave it as "Modified" it will still insert a new entity (after id fixup) to the database.
2. The new "Added" entities get duplicated in the EM after saving.
I think this is the bug that I tried to track down last we talked. You had sent me an SL solution that showed this bug but I couldn't repro it outside that solution. Would you be able to provide a console repro for this?
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 02-Nov-2011 at 2:43pm |
If I don't set it back to Added, it causes an error, at least under fake context, saying it can't find the entity.
"Unable to locate entity within the 'Fake' entity server: DealerAddress: -101"
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 03-Nov-2011 at 3:49pm |
This sounds like a bug. Does this happen upon SaveChanges? Also, what version are you using?
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 03-Nov-2011 at 4:07pm |
Yes, this happens during SaveChanges and using the latest 6.1.3.1
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 03-Nov-2011 at 6:38pm |
Ok, this is not a bug. And my statement
"If you leave it as "Modified" it will still insert a new entity (after id fixup) to the database."
is completely false. I had mentioned this as well in my earlier post. I was pretty sure I tested this which is why I posted it in the first place. But I guess between the importing and changing of EntityState, I must have confused myself. My apologies.
On the other hand, I was able to duplicate issue #2 where after calling save, the original "Added" entity with the negative temp id still exists. I've filed a bug report. Please let me know if you require a patch. I apologize again if this takes longer than anticipated.
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 03-Nov-2011 at 7:26pm |
I don't need a patch right away, my workaround seems to do the trick for now. I simply delete any modified entities after a save.
Thanks for looking into it, much appreciated. Will this and the the deletion merging issue be fixed in 6.1.4?
|
 |
DenisK
IdeaBlade
Joined: 25-Aug-2010
Posts: 715
|
Post Options
Quote Reply
Posted: 03-Nov-2011 at 7:29pm |
Sounds good.
Both issues should be fixed in 6.1.4. They're currently marked as priority.
|
 |