Removing Related Entities

Posts   
 
    
Posts: 33
Joined: 31-May-2005
# Posted on: 16-Dec-2005 19:54:20   

We are having trouble removing an entity from a collection and also having the removal of that entity be reflected in related entities. We are performing all entity removal in memory, so using DeleteEntity wouldn't help.

For example: We have a PERSON table and a related ADDRESS table. Each person can only live at 1 address, but each ADDRESS can have multiple people. For this example, let's say we have 2 people who live at Address A, and 1 person who lives at address B. If we use the Remove() function to eliminate 1 person from the PERSON entity collection, the PERSON entity collection of the related ADDRESS entity still references both people.

What's the best way to solve this issue? Particularly when we could have many related entities, not just a single table (as our simple example above)?

Thanks! Josh

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 17-Dec-2005 02:45:28   

If you are not going to persist your delete to a database then you would need to perform the remove on the already fetched collection for address also. I know this isn't what you want to do though. You could set the address.AlwaysFetchPerson property to true and then if you delete that person and persist it to storage then you would have an accurate collection on your next access of the person collection of address.

Posts: 33
Joined: 31-May-2005
# Posted on: 17-Dec-2005 05:36:04   

bclubb wrote:

If you are not going to persist your delete to a database then you would need to perform the remove on the already fetched collection for address also. I know this isn't what you want to do though.

So if we had a 7 or 8 related tables that had been fetched in the prefetch path, we'd need to do this for all 7 or 8? Seems like there'd be an easier way ...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Dec-2005 12:23:09   

JoshLindenmuth wrote:

We are having trouble removing an entity from a collection and also having the removal of that entity be reflected in related entities. We are performing all entity removal in memory, so using DeleteEntity wouldn't help.

For example: We have a PERSON table and a related ADDRESS table. Each person can only live at 1 address, but each ADDRESS can have multiple people. For this example, let's say we have 2 people who live at Address A, and 1 person who lives at address B. If we use the Remove() function to eliminate 1 person from the PERSON entity collection, the PERSON entity collection of the related ADDRESS entity still references both people.

What's the best way to solve this issue? Particularly when we could have many related entities, not just a single table (as our simple example above)?

If you want to remove a physical relation between 2 entity instances, in this case a Person instance and an address instance, either set the person's Address property to null, or remove the person from the address' Persons collection. In memory that is. If you want to persist this change, do a recursive save.

Removing it from the persons entity collection you fetched from the person table, doesn't do that removal, as it's just a bucket with a query result.

Frans Bouma | Lead developer LLBLGen Pro