Print Page | Close Window

Unexpected behavior in AttachEntity

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2535
Printed Date: 22-Jan-2026 at 5:17pm


Topic: Unexpected behavior in AttachEntity
Posted By: smi-mark
Subject: Unexpected behavior in AttachEntity
Date 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.



Replies:
Posted By: smi-mark
Date 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.


Posted By: DenisK
Date 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.


Posted By: DenisK
Date 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.



Print Page | Close Window