efcore: Insert trigger breaks SaveChanges
Ask a question
I have the setting to tell EF Core 7.0 to be aware of a trigger on my table It’s an Insert Trigger that does nothing (I’ve removed all the previous code)
‘’’ modelBuilder.Entity<Ntu>().ToTable(tb => tb.HasTrigger(“InsertTrigger”)); ‘’’
I’m inserting a record and the Insert fails. If I disable the trigger, the Insert works.
The error is below and indicates that the the key (NtuId) field is null. Obviously it is not null (because then how could it work when the trigger is disabled)
The field is an identity column so I suspect that whatever EF core is doing to save the record when there’s a trigger is causing it to lose the value in the field. Note the value is zero for inserting a new record.
An error occurred while saving the entity changes. See the inner exception for details.
2023-10-06 12:39:40.760 +01:00 [ERR] An error occurred while reading a database value for property ‘Ntu.NtuId’. The expected type was ‘System.Int32’ but the actual value was null.
2023-10-06 12:39:40.767 +01:00 [ERR] at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.Execute(IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable1 commandBatches, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList
1 entries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList1 entriesToSave) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__107_0(DbContext _, ValueTuple
2 t)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func
3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
Using EF core 6.0 with the same code I get this error: Index was outside the bounds of the array.
I’ve output the value of the field in the ‘Inserted’ table in the trigger and verified that it has the new value that should end up in the field, i.e. no longer zero, has the correct new value.
Neither error makes any sense so it looks like a bug in EF core.
(very disappointing - we’ve enough trigger problems without EF core adding more)
About this issue
- Original URL
- State: closed
- Created 9 months ago
- Comments: 15 (8 by maintainers)
You appear to be more interested in closing the ticket than investigating the problem. Do you get a bonus for closing tickets?
On Sun, 8 Oct 2023, 20:18 Shay Rojansky, @.***> wrote:
@pjpmccarthy please submit a minimal repro as I’ve requested several times.