UnitOfWork and AddForDelete problem

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 22-Sep-2006 02:54:06   

I'm trying to delete an entity along with related entities and can't get it to work. Can anyone tell me why I can delete these using Query Analyzer, but the delete won't work from a UnitOfWork.

Code for UnitOfWork delete that doesn't work.

int productId = (int)selectedRow.DataKeyValue;
ProductEntity product = new ProductEntity(productId);
product.IsNew = false;

ProductUrlEntity productUrl = new ProductUrlEntity(productId);
productUrl.IsNew = false;

ProductBlurbEntity productBlurb = new ProductBlurbEntity(productId);
productBlurb.IsNew = false;

UnitOfWork2 workUnit = new UnitOfWork2();
workUnit.AddForDelete(productBlurb);
workUnit.AddForDelete(productUrl);
workUnit.AddForDelete(product);
try
{
    ServiceFactory.GetPersistanceManager().SaveUnitOfWork(workUnit);
}
catch(Exception x)
{
    x.ToString();
    throw(x);
}

Query Analyzer delete that does work.

DELETE FROM dbo.ProductBlurb WHERE ProductID = 00000
DELETE FROM dbo.ProductUrl WHERE ProductID = 00000
DELETE FROM dbo.Product WHERE ProductID = 00000

I can see that the delete in Profiler is happening in the same order that I delete using Query Analyzer. I'm stuck trying to figure out why this isn't working.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Sep-2006 08:01:29   

In case you are using the Adapter: Does the adapter passed to the UOW commit function, has an opened transaction, that's not commited afterwards, nor you use autoCommit?

In case you are using SelfServicing: Do you pass a transaction and you don't commit it afterwards, nor you use autoCommit?

tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 22-Sep-2006 18:31:28   

I'm not using any transaction here. What you see in the code that I sent is all that is happening. It's fairly simple which is why I don't understand why it doesn't work. The exception message I get doesn't tell me much either.

Here is the sql being executed as seen in Profiler.

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;BEGIN TRANSACTION [UoWCommit]
go
exec sp_executesql N'DELETE FROM [WS2].[dbo].[ProductBlurb] WHERE ( [WS2].[dbo].[ProductBlurb].[ProductID] = @ProductID1)', N'@ProductID1 int', @ProductID1 = 3824
go
exec sp_executesql N'DELETE FROM [WS2].[dbo].[ProductUrl] WHERE ( [WS2].[dbo].[ProductUrl].[ProductUrlID] = @ProductUrlID1)', N'@ProductUrlID1 int', @ProductUrlID1 = 3824
go
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
go

Here is the exception.

During the commit of the UnitOfWork class, the delete action on an entity failed. The entity which failed is enclosed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyException: During the commit of the UnitOfWork class, the delete action on an entity failed. The entity which failed is enclosed.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 22-Sep-2006 18:59:29   

What's the INNER exception exactly?

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 22-Sep-2006 19:10:57   

Otis wrote:

What's the INNER exception exactly?

There isn't an Inner Exception, the value shown when debugging is "{}".

tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 22-Sep-2006 19:45:51   

I just thought of something that might be a problem. In the case of the code I am using if the ProductUrl doesn't exist will this cause the transaction to fail? If this is the case and the ProductUrl is causing the transaction to fail how to I get around this?

tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 23-Sep-2006 01:54:18   

OK, I found another post about deleting entities using the UnitOfWork.AddDeleteEntitiesDirectlyCall and it works great.

Here is my code now.

public void DeleteProduct(ProductEntity product)
{
    UnitOfWork2 uow = new UnitOfWork2();
    uow.AddDeleteEntitiesDirectlyCall("ProductBlurbEntity", product.GetRelationInfoProductBlurb());
    uow.AddDeleteEntitiesDirectlyCall("ProductUrlEntity", product.GetRelationInfoProductUrl());
    RelationPredicateBucket filter = new RelationPredicateBucket();
    filter.PredicateExpression.Add(ProductFields.ProductID == product.ProductID);
    uow.AddDeleteEntitiesDirectlyCall("ProductEntity", filter);
    ServiceFactory.GetPersistanceManager().SaveUnitOfWork(uow);
}