Help!! Delete and add the same entity in EntityCollection

Posts   
 
    
sirick
User
Posts: 5
Joined: 13-Dec-2004
# Posted on: 09-Jan-2005 10:42:54   

In many to many case as below:-

Party: PartyID int, PartyName varchar

PartyRole: PartyID int PartyRoleTypeID int

PartyRoleType: PartyRoleTypeID int PartyRoleTypeName varchar

I designed my app as to listbox to add and remove role type for specified party. And my test is remove role type from assigned list to available role type list. And add the same role type back to assigned list. Then save.

There was an error occured about constraint violation in PartyRole Table.

How to solve this problem.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Jan-2005 12:44:36   

Please post the code in which you re-construct the graph and save it.

Frans Bouma | Lead developer LLBLGen Pro
sirick
User
Posts: 5
Joined: 13-Dec-2004
# Posted on: 09-Jan-2005 13:56:52   

public void RefreshPartyRoles()
{
    adapter = new DataAccessAdapter();
    EntityCollection PartyRoles = partyEntity.PartyRoles;
    adapter.FetchEntityCollection(PartyRoles, partyEntity.GetRelationInfoPartyRoles());
    //
    // After fetched, the result collection marked with AllowRemove = false; the I try to force it removable but it's still not work
    //
    PartyRoles.AllowRemove = true;
}

public void RemovePartyRole(PartyRoleTypeEntity partyRoleType)
{
    PartyRoleEntity partyRole = (PartyRoleEntity)partyEntity.partyRoles[partyRoles.Find(new EntityPropertyDescriptor2(EntityFieldFactory.Create(PartyRoleFieldIndex.PartyRoleTypeID), typeof(PartyRoleEntity), false), partyRoleType.IdentityID)];  
    if (partyRole != null) {
        partyEntity.PartyRoles.Remove(partyRole);   
    }
}

public void AddPartyRole(PartyRoleTypeEntity partyRoleType)
{
    partyEntity.PartyRoles.Add(new PartyRoleEntity(partyRoleType.ID, partyEntity.ID));
}

After that call adapter.SaveEntity with recurse option

Sirichoke K.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Jan-2005 09:23:24   

sirick wrote:


public void RefreshPartyRoles()
{
    adapter = new DataAccessAdapter();
    EntityCollection PartyRoles = partyEntity.PartyRoles;
    adapter.FetchEntityCollection(PartyRoles, partyEntity.GetRelationInfoPartyRoles());
    //
    // After fetched, the result collection marked with AllowRemove = false; the I try to force it removable but it's still not work
    //
    PartyRoles.AllowRemove = true;
}

You set it to false and you still can remove an item?


public void RemovePartyRole(PartyRoleTypeEntity partyRoleType)
{
    PartyRoleEntity partyRole = (PartyRoleEntity)partyEntity.partyRoles[partyRoles.Find(new EntityPropertyDescriptor2(EntityFieldFactory.Create(PartyRoleFieldIndex.PartyRoleTypeID), typeof(PartyRoleEntity), false), partyRoleType.IdentityID)];  
    if (partyRole != null) {
        partyEntity.PartyRoles.Remove(partyRole);   
    }
}

public void AddPartyRole(PartyRoleTypeEntity partyRoleType)
{
    partyEntity.PartyRoles.Add(new PartyRoleEntity(partyRoleType.ID, partyEntity.ID));
}

After that call adapter.SaveEntity with recurse option

Sirichoke K.

Removing an entity from a collection doesn't mean it is removed from the database as well. Removing from a collection can mean: you don't want to use it at that moment for example. If you want to remove it from the database, you have to call adapter.DeleteEntity() or if you want to delete a complete collection call adapter.DeleteEntityCollection()

Frans Bouma | Lead developer LLBLGen Pro
sirick
User
Posts: 5
Joined: 13-Dec-2004
# Posted on: 10-Jan-2005 10:09:28   

Otis,

Our scenario is save all changes from the top most object (partyEntity). This means, we cannot do it. But can only add new one.

On our gui design, if user change his/her mind, they always can cancel this action.

Do you have any idea with our scenario.

Thanks. Sirichoke K.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Jan-2005 11:36:51   

Use a unitofwork2 object. If you want to remove an entity, add it to the unitofwork with AddForDelete(). You could also keep track of which entities to delete from the db in a separate collection. And add that collection to the UnitOfWork2 object with AddCollectionForDelete() and you then also add partyEntity using AddForSave() and then you commit the UnitOfWork2 object using its Commit() method. If the user cancels, simply throw away the unitofwork object and the collection with the entities to remove.

Still you then have a modified entitycollection with removed entities. It's then wise to refetch the partyEntity if you want to cache it in the client.

Frans Bouma | Lead developer LLBLGen Pro
sirick
User
Posts: 5
Joined: 13-Dec-2004
# Posted on: 12-Jan-2005 02:03:24   

Otis,

Which order will UnitOfWork2 do when call commit?

Add/Save first and the Delete (Both Entity/Collection)?

Sirickwink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 12-Jan-2005 10:37:46   

sirick wrote:

Otis,

Which order will UnitOfWork2 do when call commit?

Add/Save first and the Delete (Both Entity/Collection)? Sirickwink

All entities in collections added will first be added to the lists of entities internally. So all entities in collections for save will be added to the lists for insert or update and all entities in collections for delete will be added to the list of entities to delete.

Then it first inserts, then updates and then deletes. Inserts / update distinction is done based on the IsNew flag on the entity.

Frans Bouma | Lead developer LLBLGen Pro