EF Core
EF Core

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:


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.

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)
        .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!

Last modified: May 2, 2023



Write a Reply or Comment

Your email address will not be published.