Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlException when Audit table has triggers #762

Open
sven5 opened this issue Apr 14, 2023 · 1 comment
Open

SqlException when Audit table has triggers #762

sven5 opened this issue Apr 14, 2023 · 1 comment
Assignees

Comments

@sven5
Copy link

sven5 commented Apr 14, 2023

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 ---
@JonathanMagnan JonathanMagnan self-assigned this Apr 14, 2023
@JonathanMagnan
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants