New Posts New Posts RSS Feed: Unexpected behavior in AttachEntity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Unexpected behavior in AttachEntity

 Post Reply Post Reply
Author
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 Topic: Unexpected behavior in AttachEntity
    Posted: 28-Feb-2011 at 1:03pm
According to the documentation, calling CreateEntity should result in a detached entity, and it should stay detatched until AddToManager or AttachEntity is called. Setting a relation property causes it to be 'added'. See below:

       private AccountContact AddAccountContact(EntityManager manager, Account account, string name, string email)
        {
            var contact = manager.CreateEntity<AccountContact>();
            contact.AccountContactId = Guid.NewGuid(); // Detached (correct)
            contact.Account =account; //Added (invalid)
            contact.Name = name;
            contact.EmailAddress = email;
            manager.AttachEntity(contact); //Stays as "Added" rather than unmodified.
            return contact;
        }

Is this the expected behavior? I am using this as sample data so I don't really want it to show as Added.

It looks like ScalarEntityReference.SetEntity is calling "AttachIfPossible" which is adding it to the manager rather than keeping it in a detached state.


Edited by smi-mark - 28-Feb-2011 at 1:10pm
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: 28-Feb-2011 at 8:25pm
I thought using a fake composition context might bypass this, which it seems to have done, but I found another issue.

Something is very strange because using this with a fake composition context, there is no original value map.

This would all work fine, I'm sure, if it was from a database, but right now I am working in model first, there is no database as such.

I was able to "fix" it by creating a simple console app and overriding the EntityState and original value map and then save the cache state, which the SL app now reads and works fine with.

Perhaps I'm just doing something wrong, but the behavior seems a little weird.
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: 01-Mar-2011 at 12:51pm
Hi smi-mark;

If you're assigning contact.Account to an "account" entity that is already attached to an EntityManager, then "contact" will automatically be added to that EntityManager as well. 

The AttachEntity seems to change an entity's state to Unchanged when the entity is still Detached. But when the entity's state is Added, the AttachEntity call seems to not change the entity's state as you observed. I have to confirm this one with one of the lead developers to see if this is the expected behavior and get back to you.

I couldn't repro your issue with the fake composition context having no original values map. Could you give me a code snippet showing how you create and populate the fake EM? Please give me the DevForce version you're using as well.
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: 01-Mar-2011 at 1:07pm
Hi smi-mark;

The AttachEntity is the expected behavior. As the API says, it will only add a detached entity and change its state to an Unchanged state. The entity will be ignored if it already belongs to that EntityManager or an exception will be thrown if you're trying to attach an entity that already belongs to another EntityManager.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down