New Posts New Posts RSS Feed: Possible Bug - Import entities
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Possible Bug - Import entities

 Post Reply Post Reply Page  123>
Author
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Topic: Possible Bug - Import entities
    Posted: 20-Jan-2012 at 2:19pm
Thanks for the sample smi-dan. That's why I couldn't repro it. This issue is fixed in 6.1.5.

Edited by DenisK - 20-Jan-2012 at 2:20pm
Back to Top
smi-dan View Drop Down
Newbie
Newbie


Joined: 26-Jan-2011
Location: Dallas, Texas
Posts: 3
Post Options Post Options   Quote smi-dan Quote  Post ReplyReply Direct Link To This Post Posted: 19-Jan-2012 at 9:12pm
Hi DenisK, 

I've gone ahead and created a sample project that demonstrates the issue. 


You'll note that the Note.Text makes it through the RestoreCacheState but none of the ComplexType properties make it through.

I am using DevForce 6.1.4.0
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 19-Jan-2012 at 3:28pm
Hi smi-dan,

No, that's not the intended behavior. We have tests here that cover this scenario and they all pass. If you can send me your edmx containing only the Note entity and its Audit complex type, I'll try to repro it here. Please let me know as well the version of DevForce you're using.

Thank you.
Back to Top
smi-dan View Drop Down
Newbie
Newbie


Joined: 26-Jan-2011
Location: Dallas, Texas
Posts: 3
Post Options Post Options   Quote smi-dan Quote  Post ReplyReply Direct Link To This Post Posted: 19-Jan-2012 at 1:42pm
Following on from this thread - the code we ended up using for our Merge method is as follows; 

            sourceRepository.Manager.FindEntities<Entity>(EntityState.Added)
                .ForEach(e =>
                                 {
                                     e.EntityAspect.AcceptChanges();
                                     e.EntityAspect.SetModified();
                                 });

            var restoreStrategy = new RestoreStrategy(false, false, MergeStrategy.OverwriteChanges);
            var data = sourceRepository.Manager.CacheStateManager.GetCacheState();
            Manager.CacheStateManager.RestoreCacheState(data, restoreStrategy);
            Manager.FindEntities<Entity>(EntityState.Modified)
                .Where(e => (int)e.EntityAspect.EntityKey.Values[0] < 0)
                .ForEach(e => e.EntityAspect.EntityState = EntityState.Added);


We have a Note entity which has a complex type Audit. After GetCacheState() the EntityCacheState preserves the state of Audit however after the call to RestoreCacheState() Audit's properties are null. 

Is this a bug with RestoreCacheState or intended behavior? Is there a way to preserve the state of Audit?
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 03-Nov-2011 at 7:29pm
Sounds good.

Both issues should be fixed in 6.1.4. They're currently marked as priority.
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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?


Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 03-Nov-2011 at 4:07pm
Yes, this happens during SaveChanges and using the latest 6.1.3.1
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 03-Nov-2011 at 3:49pm
This sounds like a bug. Does this happen upon SaveChanges? Also, what version are you using?
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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"
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.

For best practices suggestions, you might want to read this blog post by Ward on using DevForce with Sandbox editors, if you haven't. http://neverindoubtnet.blogspot.com/2010/12/sandbox-editors-with-clientui-contacts.html

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?



Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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(falsefalseMergeStrategy.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
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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?

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 27-May-2011 at 12:02pm
I appreciate the help Mark. Thanks!
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply Page  123>

Forum Jump Forum Permissions View Drop Down