EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

SqlException when Audit table has triggers

Open sven5 opened this issue 2 years ago • 1 comments

1. Description

An exception is thrown when the AuditEntry table has a trigger. We're using Azure SQL database.

2. Exception

Microsoft.Data.SqlClient.SqlException (0x80131904): The target table 'AuditEntrySet' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.

Exception message:

Failed executing DbCommand (0ms) [Parameters=[@p0='?' (Size = 255), @p1='?' (DbType = DateTime2), @p2='?' (Size = 255), @p3='?' (Size = 255), @p4='?' (DbType = Int32), @p5='?' (Size = 255)], CommandType='Text', CommandTimeout='30']
      SET IMPLICIT_TRANSACTIONS OFF;
      SET NOCOUNT ON;
      INSERT INTO [AuditEntrySet] ([CreatedBy], [CreatedDate], [EntitySetName], [EntityTypeName], [State], [StateName])
      OUTPUT INSERTED.[AuditEntryID]
      VALUES (@p0, @p1, @p2, @p3, @p4, @p5);

An exception occurred in the database while saving changes for context type 'DataContext'.
      Microsoft.EntityFrameworkCore.DbUpdateException: Could not save changes because the target table has database triggers. Please configure your entity type accordingly, see https://aka.ms/efcore-docs-sqlserver-save-changes-and-triggers for more information.
       ---> Microsoft.Data.SqlClient.SqlException (0x80131904): The target table 'AuditEntrySet' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.
         at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
         at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
         at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
         at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
         at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
         at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
         at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
         at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
         at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
         at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
         at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
         at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
         at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
      ClientConnectionId:1de58170-c583-49ad-8a4e-8e063c0dee79
      Error Number:334,State:1,Class:16
         --- End of inner exception stack trace ---

sven5 avatar Apr 14 '23 11:04 sven5

Hello @sven5 ,

I believe you are currently using EF Core 7.

Since EF Core 7, there is a huge breaking change on their side and you have now to specify which table has a trigger.

See: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/breaking-changes#sql-server-tables-with-triggers-or-certain-computed-columns-now-require-special-ef-core-configuration

The current bug (unless this is something else in your case) is not related to our library as you can reproduce the same behavior by simply adding a row to this table and calling the SaveChanges.

Best Regards,

Jon


Sponsorship Help us improve this library

Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsDapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval Function

JonathanMagnan avatar Apr 14 '23 14:04 JonathanMagnan