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

Document SQL Server + triggers breaking change #3887

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions entity-framework/core/providers/sql-server/misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Miscellaneous - Microsoft SQL Server Database Provider - EF Core
description: Miscellaneous for the Microsoft SQL Server database provider
author: roji
ms.date: 06/07/2021
uid: core/providers/sql-server/misc
---
# Miscellaneous notes for SQL Server

## SaveChanges and database triggers

Starting with EF Core 7.0, EF Core saves changes to the database with significantly optimized SQL; unfortunately, this technique is not supported on SQL Server if the target table has database triggers. For more information on this SQL Server limitation, see the documentation on the [OUTPUT clause](/sql/t-sql/queries/output-clause-transact-sql).

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:

[!code-csharp[Main](../../../../samples/core/SqlServer/Misc/TriggersContext.cs?name=TriggerConfiguration&highlight=4)]

Note that doing this doesn't actually make EF Core create or manage the trigger in any way - it currently only informs EF Core that triggers are present on the table. As a result, any trigger name can be used.
34 changes: 34 additions & 0 deletions entity-framework/core/what-is-new/ef-core-7.0/breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,37 @@ uid: core/what-is-new/ef-core-7.0/breaking-changes
# Breaking changes in EF Core 7.0

API and behavior changes have the potential to break existing applications updating to EF Core 7.0.0 will be documented here.

## Summary

| **Breaking change** | **Impact** |
|:------------------------------------------------------------------------------------------------------------ | ----------- |
| [SQL Server tables with triggers now require special EF Core configuration](#sqlserver-tables-with-triggers) | High |

## High-impact changes

<a name="sqlserver-tables-with-triggers"></a>

### SQL Server tables with triggers now require special EF Core configuration

[Tracking Issue #27372](https://github.com/dotnet/efcore/issues/27372)

#### Old behavior

Previous versions of the SQL Server 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.

#### Why

The performance improvements linked to the new method are significant enough that it's important to bring them to users by default. At the same time, we estimate usage of database triggers in EF Core applications to be low enough that the negative breaking change consequences are outweighed by the performance gain.

#### 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:

[!code-csharp[Main](../../../../samples/core/SqlServer/Misc/TriggersContext.cs?name=TriggerConfiguration&highlight=4)]

Note that doing this doesn't actually make EF Core create or manage the trigger in any way - it currently only informs EF Core that triggers are present on the table. As a result, any trigger name can be used.
2 changes: 2 additions & 0 deletions entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@
href: core/providers/sql-server/spatial.md
- name: Specify Azure SQL Database options
href: core/providers/sql-server/azure-sql-database.md
- name: Miscellaneous
href: core/providers/sql-server/misc.md
- name: SQLite
items:
- name: Overview
Expand Down
7 changes: 7 additions & 0 deletions samples/core/SqlServer/Misc/Blog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SqlServer.Faq;

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
17 changes: 17 additions & 0 deletions samples/core/SqlServer/Misc/TriggersContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

using Microsoft.EntityFrameworkCore;

namespace SqlServer.Faq;

public class TriggersContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region TriggerConfiguration
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.ToTable(tb => tb.HasTrigger("SomeTrigger"));
}
#endregion
}
2 changes: 1 addition & 1 deletion samples/core/SqlServer/SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-preview.4.22229.2" />
</ItemGroup>

</Project>