EntityFrameworkCore.DbUpdateException: Could not save changes because the target table has database triggers. Please configure your entity type accordingly

After migration from .NET Core 3.1 to the newest .NET 7 and Entity Framework 7 I received on the production environment the following error message for a few entities during data updates:

EntityFrameworkCore.DbUpdateException: Could not save changes because the target table has database triggers. Please configure your entity type accordingly

On my development environment everything works as expected without any error. But the production database has some Triggers attached to some SQL Tables ... so the error occurred only in production - very frustrating :-(

Thats why: Development and Production environment should be identical.

The reason for the error is described in the MSDN documentation

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

The problem can be solved very easy. Just open the database context class in your project and add for each table with one or more trigger the following statement:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  ...
  modelBuilder.Entity<YOUR-ENTITY-CLASS-NAME>().ToTable(tb => tb.HasTrigger("TriggerName"));
  ...
}

The name of the trigger is not important regarding the Entity Framework 7.0 version documentation. Maybe this will be changed in the future. In case the most of the tables have a trigger attached see the MSDN documentation for an alternative option to inform the Entity Framework about the triggers.

Happy coding again :-)

Kommentare

Beliebte Posts aus diesem Blog

Exchange Online: Schutzregel für E-Mail Weiterleitung

Vertikaler Bereich deaktiviert

Power Automate: Abrufen von SharePoint Listenelementen mit ODATA-Filter