How do I copy a collection to an entity?

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 09-Apr-2008 21:09:54   

Hi,

I have an entity collection that I would like to assign to a parent entity's collection property. I can't do this because the collection property is read only.

How can I transfer the child entities accross without emptying out the collection?

Is it even possible to have an entity in two different collections?

Cheers, Ian.

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 10-Apr-2008 11:00:32   

               for (int i= 0; i < SkinAttributes1.SkinAttributesCollection.Count; i++)
                {
                    SkinAttributeEntity attributeEntity = SkinAttributes1.SkinAttributesCollection[i];

                    if (attributeEntity.IsNew)
                    {
                        attributeEntity.SkinId = itemID;
                    }

                    entity.SkinAttribute.Add(attributeEntity);
                }

I'm finding that sometimes the attributeEntity is removed from SkinAttributesCollection and sometimes its not. It seems to depend on whether entity is new or not!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Apr-2008 11:21:46   

I'm finding that sometimes the attributeEntity is removed from SkinAttributesCollection

SkinAttributesCollection, is this the source collection or the destination one?

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 10-Apr-2008 12:23:28   

Its the source. Why do you ask? Its clear from the code that SkinAttributesCollection is being iterated over and each of its elements is being added to a different collection.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Apr-2008 12:28:55   

I wanted to make sure in which collection do you fing the entities removed.

SkinAttributes1.SkinAttributesCollection Or entity.SkinAttribute

Since both are SkinAttributesCollections.

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 10-Apr-2008 13:58:47   

Ok cool.

Well what happens is that, during the loop, sometimes the count of

SkinAttributes1.SkinAttributesCollection

goes down where as other times it does not. And yet no item from this collection is explicitly removed. There must be some rule I'm unaware of.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Apr-2008 15:33:15   

SkinAttributes1.SkinAttributesCollection

Would you please sketch out the DDL and refrential relation for yje above? Is the attributeEntity.SkinId the FK used in the above relation?

If so then it seems that using the code below you are derefrencing the attribute entity from its related SkinAttributes1 that's why it is removed from the related collection.

                if (attributeEntity.IsNew)
                {
                    attributeEntity.SkinId = itemID;
                }

Is this correct? if you want to test it just comment the above code. Also what is the value of itemID? from where do you get this value?

Either this or entity is of the same type as SkinAttributes1, so you are clearly dereferencing the entity from the SkinAttributes1 related SkinAttributesCollection to that of the entity.

SkinAttributeEntity attributeEntity = SkinAttributes1.SkinAttributesCollection[i];

                if (attributeEntity.IsNew)
                {
                    attributeEntity.SkinId = itemID;
                }

                entity.SkinAttribute.Add(attributeEntity);
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 10-Apr-2008 22:50:14   

Yes attributeEntity.SkinId is the fk.

There's a SkinEntity and it contains a collection of SkinAttributes.

Here's what happens.

A SkinEntity is fetched and its attributes are pre-fetched. The skin is bound to a web page and then discarded. The collection is bound to a webcontrol and is stored in session.

When a user submits the web page, the skin entity is re-created and I'm trying to plug the attributes back in. I can't directly assign because the skin's property is read only so I figure I have to copy them across one at a time.

If an attributes is new, then just adding it to the skin's attributes collection doesn't seem to be enough to set up the relationship. (I'd have thought it would be else why else is the attribute in the collection?) So instead I assign the new attribute's SkinID to the key for the webpage which identifies the skin.

From what you're saying this will make the attribute be removed fom the source collection. Well it would be helpful if it wasn't.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2008 12:04:41   

If an attributes is new, then just adding it to the skin's attributes collection doesn't seem to be enough to set up the relationship. (I'd have thought it would be else why else is the attribute in the collection?) So instead I assign the new attribute's SkinID to the key for the webpage which identifies the skin.

If an attribute is new, the FK-PK syncronization would be taken care of when saving the graph. So if you try to save the skin recursively with its attributes they would be wired up in the database.

doesn't seem to be enough to set up the relationship

Would you please post your test code which made you drive to the above conclusion.

From what you're saying this will make the attribute be removed fom the source collection. Well it would be helpful if it wasn't.

Well if the source collection is associated/related to a different Skin entity, then definitly it would be removed from the source collection.

But if the source collection was not wired up with any skin entity, then the entity won't be removed from the source collection.

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 11-Apr-2008 14:32:38   

Would you please post your test code which made you drive to the above conclusion.

Works now. Thanks Walaa for your help. sunglasses

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 13-Apr-2008 22:45:53   

Hi, is there a way of severing a collection's ties with its parent entity?

So that if I do copy an entity from the collection to another, it doesn't get removed from the source?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Apr-2008 08:39:30   

You may clone it (create a new instance and copy its data).

If you search the forum for "clone" you will get some useful threads.