zzzprojects / Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
https://dapper-plus.net/
383 stars 85 forks source link

Exception when using BulkMerge with auditing when triggers are enabled #73

Closed atrousb2 closed 3 years ago

atrousb2 commented 3 years ago

Hi, guys!

In our current project, we use auditing functionality:

private static void UseAuditing(BulkOperation operation, List<AuditEntry> auditEntries)
{
     operation.AuditEntries = auditEntries;
     operation.UseAudit = true;
}

together with the BulkMerge method for creating and updating entities.

The thing is once a trigger on a table is enabled, we got the following exception:

The target table 'DestinationTable' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.

Can it be fixed? And is there a workaround for now?

JonathanMagnan commented 3 years ago

Hello @atrousb2 ,

This issue happens if our library cannot discover that your table has some trigger, for example, if the user has not the right (Otherwise, it will automatically handle it).

If that's only this table, you can force it to use the right resolution this way:

DapperPlusManager.Entity<EntityWithTrigger>().UseBulkOptions(x =>
{
        // all logic such as if our library had discovered trigger will be logic
    x.ForceTriggerResolution = true;
});

// or
DapperPlusManager.Entity<EntityWithTrigger>().UseBulkOptions(x =>
{
        // will force the output in a select, should not be needed if you already specified ForceTriggerResolution = true
    x.ForceSelectOutput = true;
});

You can also enable it globally this way:

BulkOperationManager.BulkOperationBuilder = operation =>
{
    operation.ForceTriggerResolution = true;
}

Let me know if that solution worked.

Best Regards,

Jon

atrousb2 commented 3 years ago

@JonathanMagnan, yeah, it worked, thank you! :)

JonathanMagnan commented 3 years ago

Awesome @atrousb2

Don't hesitate to contact us with any questions, issues or feedback!

Best regards,

Jon