ORMConcurrencyException

Posts   
 
    
DaveW_396
User
Posts: 5
Joined: 15-Jul-2009
# Posted on: 15-Jul-2009 22:59:03   

In a unit test I was testing what the outcome was if I tried to delete an entity that didn't exist in the database. I get the below exception when I add an entity into a EntityCollection and call DeleteEntityCollection on the DataAccessAdaptor.

platform: Windows XP SP3 c# .net v3.5

((SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyException)(e)){"During the commit of the UnitOfWork class, the delete action on an entity failed. The entity which failed is enclosed."}

Code snip example:

TestEntity doesNotExist = new TestEntity (); doesNotExist.TestId = 99090; doesNotExist.IsNew = false;

EntityCollection<TestEntity> toDelete = new EntityCollection<TestEntity>(); toDelete.Add(doesNotExist);

using (DataAccessAdapter adapter = new DataAccessAdapter()) { int count = adapter.DeleteEntityCollection (toDelete); //<---throws exception }

If I do not set the IsNew property on the entity then I do not get an exception. What is happening when I set the IsNew property causing this exception? This error also does not happen if I send the same entity for deletion using adapter.DeleteEntity(doesNotExist);

Any thoughts on why this flag throws an error under this condition?

DaveW_396
User
Posts: 5
Joined: 15-Jul-2009
# Posted on: 16-Jul-2009 00:17:58   

I have a followup question based on my testing. Why is there a difference in the deletion criteria when using the DataAccessAdaptor's DeleteEntity() and DeleteEntityCollection()?

In both cases I tested the IsDirty property is set to true (because I set the PK I assume), but:

DeleteEntity() will delete an entity from the DB if the IsNew property is set to true or false on the entity.

DeleteEntityCollection() will delete an entity from the DB only if the IsNew property is set to false.

DaveW_396
User
Posts: 5
Joined: 15-Jul-2009
# Posted on: 16-Jul-2009 00:18:12   

I have a followup question based on my testing. Why is there a difference in the deletion criteria when using the DataAccessAdaptor's DeleteEntity() and DeleteEntityCollection()?

In both cases I tested the IsDirty property is set to true (because I set the PK I assume), but:

DeleteEntity() will delete an entity from the DB if the IsNew property is set to true or false on the entity.

DeleteEntityCollection() will delete an entity from the DB only if the IsNew property is set to false.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Jul-2009 06:41:18   

Hi Dave,

DaveW_396 wrote:

I have a followup question based on my testing. Why is there a difference in the deletion criteria when using the DataAccessAdaptor's DeleteEntity() and DeleteEntityCollection()?

DeleteEntityCollection() creates -internally- a UnitOfWork to commit all deletes together. When the UnitOfWork is commited, it internally calls DeleteEntity for each delete call. If one of these calls returns "False" (something went wrong) it raise the exception. DeleteEntity() doesn't thrown exception but it returns 'true' or 'false' (true if the delete was succesful, otherwise false).

DaveW_396 wrote:

In both cases I tested the IsDirty property is set to true (because I set the PK I assume),

Yes, coz you indeed set the PK.

DaveW_396 wrote:

DeleteEntity() will delete an entity from the DB if the IsNew property is set to true or false on the entity. DeleteEntityCollection() will delete an entity from the DB only if the IsNew property is set to false.

Well. This is what happens: At DeleteEntity(), the method doesn't check the _IsNew _flag, but it indeed will returns true or false (it will return false is for example, the entity doesn't exists). It something else happened (failed, due to the delete restriction, for instance) it will throw an ORMConcurrencyException.

Now, DeleteEntityCollection() will add each entityToDelete to a UnitOfWork queue. When some entity is added to the Delete Queue, it indeed checks whether or not the entity "Is New". So if the entity is new, it isn't added, so no deletes are performed.

Hope my explanation was clear.

David Elizondo | LLBLGen Support Team