Otis wrote:
The only thing I can think of is that if you delete an entity, it will fail because the delete query of the supertype fails as the trigger already removed it: the batch query will fail in total if 1 of the queries for the supertypes fails (returns 0 rows affected).
The only thing you gain is when you delete entities directly, as that's not supported in the ORM for these entities. The runtime will always run the queries for the supertype rows as well, so I'm not sure whether it will gain you much in the end.
So not sure why it works, perhaps the delete queries for the supertype rows still see the rows already removed by the trigger on the subtype, but that's just guessing.
That's worrying. I'd better make what I am doing will be OK.
This is a test I'm running:-
[Test]
public void TestCreateAndAllocateCommissionStatementThenDeleteAllocation()
{
PerformTestInTransactionWithRollback(adapter =>
{
var info = DoCreateCreateAndAllocateCommissionStatement(adapter);
adapter.DeleteEntity(info.Allocation);
Assert.AreEqual(0, adapter.GetDbCount<AllocationEntity>()); Assert.AreEqual(0, adapter.GetDbCount<AllocationItemEntity>());
});
}
And the SQL generated by the DeleteEntity method is this:-
--BatchAction Query (Count=2):
--Action Query #1:
DECLARE @p1 int; SET @p1='6'
DELETE
FROM
[Allocation]
WHERE
( [Allocation].[ID] = @p1)
--Action Query #2:
DECLARE @p2 int; SET @p2='6'
DELETE
FROM
[AccountsTransaction]
WHERE
( [AccountsTransaction].[ID] = @p2)
Now I just recreated this and ran the above manually so that I could see the output:-
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(1 row(s) affected)
(0 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
It gets a bit difficult to work out which result goes with which command/trigger but I know the last two non-zeros would be for NominalTransaction/NominalItem that have a Cascade deleteo.
This is the output with Tracing switched to Verbose
Method Enter: DataAccessAdapterBase.DeleteEntity(2)
Active Entity Description:
Entity: TIPS.DAL.EntityClasses.AllocationEntity. ObjectID: 36b05e8b-dab9-496e-93e3-5c9fb6075ccf
PrimaryKey field: ID. Type: System.Int32. Value: 12
PrimaryKey field: ID. Type: System.Int32. Value: 12
--BatchAction Query (Count=2):
--Action Query #1:
DECLARE @p1 int; SET @p1='12'
DELETE
FROM
[Allocation]
WHERE
( [Allocation].[ID] = @p1)
--Action Query #2:
DECLARE @p2 int; SET @p2='12'
DELETE
FROM
[AccountsTransaction]
WHERE
( [AccountsTransaction].[ID] = @p2)
Method Enter: DataAccessAdapterBase.ExecuteActionQuery
Method Enter: DataAccessAdapterBase.OpenConnection
Method Exit: DataAccessAdapterBase.OpenConnection
Method Enter: Query.ReflectOutputValuesInRelatedFields
Method Exit: Query.ReflectOutputValuesInRelatedFields: no parameter relations.
Executed Sql Query:
Query: DELETE FROM [TIPS].[dbo].[Allocation] WHERE ( [TIPS].[dbo].[Allocation].[ID] = @p1)
Parameter: @p1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 12.
Executed Sql Query:
Query: DELETE FROM [TIPS].[dbo].[Allocation] WHERE ( [TIPS].[dbo].[Allocation].[ID] = @p1)
Parameter: @p1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 12.
Executed Sql Query:
Query: DELETE FROM [TIPS].[dbo].[AccountsTransaction] WHERE ( [TIPS].[dbo].[AccountsTransaction].[ID] = @p2)
Parameter: @p2 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 12.
Executed Sql Query:
Query: DELETE FROM [TIPS].[dbo].[AccountsTransaction] WHERE ( [TIPS].[dbo].[AccountsTransaction].[ID] = @p2)
Parameter: @p2 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 12.
Method Exit: DataAccessAdapterBase.ExecuteActionQuery
Method Exit: DataAccessAdapterBase.DeleteEntity(2)
Method Enter: DataAccessAdapterBase.Rollback
Method Enter: DataAccessAdapterBase.Reset
Method Exit: DataAccessAdapterBase.Reset
Method Exit: DataAccessAdapterBase.Rollback
If I run this through SQL Debugger, I can see @@ROWCOUNT=1 after the first delete and then @@ROWCOUNT=0 after the second delete which is exactly what you expected.
Does LLBLGen get @@ROWCOUNT back or some other value?
I get a bit lost because I can't step into LLBLGen code properly (I can see stuff in the Local window but the source code stepping seems to be random - probably because it is a Resharper reverse compile) but I'm guessing there are two ActionQuery objects, both deletes, wrapped up in a BatchActionTransaction.
However, the BatchActionTransaction has its _quitOnPartyFailure set to False and so will work correctly based on the first delete returning 1.