We upgraded Entity framework in a .NET Core project to 7.0 and suddenly inserts into a SQL Table were no longer working – nothing got inserted and there was no error.
Let’s check the breaking changes document:
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
Old behavior
Previous versions of the SQL Server provider saved changes via a less efficient technique which always worked.
New behavior
By default, EF Core now saves changes via a significantly more efficient technique; unfortunately, this technique is not supported on SQL Server if the target table has database triggers, or certain types of computed columns. See the SQL Server documentation for more details.Mitigations
You can let EF Core know that the target table has a trigger; doing so will revert to the previous, less efficient technique. This can be done by configuring the corresponding entity type as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity()
.ToTable(tb => tb.HasTrigger("SomeTrigger"));
}
So I duly went and copied this line into all my OnModelCreating Entity entries for the tables in question, and the context.SaveChanges();
all now worked again!
Note we’re literally using “SomeTrigger”, doesn’t feel right and doesn’t feel like this is a many-year solution at all!
Comments