Skip to content

Commit 11449dc

Browse files
authored
Improvements and tweaks to the SQL Server docs (#5064)
1 parent b707e72 commit 11449dc

File tree

5 files changed

+125
-53
lines changed

5 files changed

+125
-53
lines changed

.openpublishing.redirection.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@
489489
"source_path": "entity-framework/core/providers/cosmos/functions.md",
490490
"redirect_url": "/ef/core/providers/cosmos/querying",
491491
"redirect_document_id": false
492-
}
492+
},
493+
{
494+
"source_path": "entity-framework/core/providers/sql-server/azure-sql-database.md",
495+
"redirect_url": "/ef/core/providers/sql-server/misc",
496+
"redirect_document_id": false
497+
}
493498
]
494499
}

entity-framework/core/providers/sql-server/azure-sql-database.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

entity-framework/core/providers/sql-server/index.md

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,103 @@ Install-Package Microsoft.EntityFrameworkCore.SqlServer
2727

2828
***
2929

30-
> [!NOTE]
31-
> The provider references Microsoft.Data.SqlClient (not System.Data.SqlClient). If your project takes a direct dependency on SqlClient, make sure it references the Microsoft.Data.SqlClient package.
30+
## Usage and configuration
31+
32+
Once your project references the nuget package, configure EF for SQL Server as follows:
33+
34+
### [SQL Server (on-premises)](#tab/sqlserver)
35+
36+
```c#
37+
public class MyContext : DbContext
38+
{
39+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
40+
{
41+
optionsBuilder.UseSqlServer("<CONNECTION STRING>");
42+
}
43+
}
44+
```
45+
46+
When using EF with dependency injection (e.g. ASP.NET), use the following:
47+
48+
```c#
49+
var builder = WebApplication.CreateBuilder(args);
50+
builder.Services.AddDbContext<MyContext>(options =>
51+
options.UseSqlServer(builder.Configuration.GetConnectionString("MyContext")));
52+
```
3253

33-
> [!TIP]
34-
> The Microsoft.Data.SqlClient package ships more frequently than the EF Core provider. If you would like to take advantage of new features and bug fixes, you can add a direct package reference to the latest version of Microsoft.Data.SqlClient.
54+
### [Azure SQL](#tab/azure-sql)
3555

36-
> [!WARNING]
37-
> The async implementation of [Microsoft.Data.SqlClient](https://github.com/dotnet/SqlClient) unfortunately has some known issues (e.g. [#593](https://github.com/dotnet/SqlClient/issues/593), [#601](https://github.com/dotnet/SqlClient/issues/601), and others). If you're seeing unexpected performance problems, try using sync command execution instead, especially when dealing with large text or binary values.
56+
```c#
57+
public class MyContext : DbContext
58+
{
59+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
60+
{
61+
optionsBuilder.UseAzureSql("<CONNECTION STRING>");
62+
}
63+
}
64+
```
3865

39-
## Usage
66+
When using EF with dependency injection (e.g. ASP.NET), use the following:
4067

41-
Starting with EF 9, it's recommended to use `UseAzureSql` and `UseAzureSynapse` to specify that you're connecting to Azure SQL or Azure Synapse Analytics specifically, and `UseSqlServer` to specify that you're connecting to on-premises SQL Server; doing so allows the provider to optimize for and properly support these platforms. It's also recommended to use `UseCompatibilityLevel` method to specify the compatibility level so that the generated SQL is compatible and/or uses the latest possible features.
68+
```c#
69+
var builder = WebApplication.CreateBuilder(args);
70+
builder.Services.AddDbContext<MyContext>(options =>
71+
options.UseSqlServer(builder.Configuration.GetConnectionString("MyContext")));
72+
```
4273

4374
> [!NOTE]
44-
> UseAzureSql and UseAzureSynapse methods were introduced in EF Core 9.0.
75+
> <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseAzureSql*> was introduced in EF Core 9.0. When using an older version, use <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer*> instead.
76+
77+
### [Azure Synapse Analytics](#tab/azure-synapse)
78+
79+
```c#
80+
public class MyContext : DbContext
81+
{
82+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
83+
{
84+
optionsBuilder.UseAzureSynapse("<CONNECTION STRING>");
85+
}
86+
}
87+
```
88+
89+
When using EF with dependency injection (e.g. ASP.NET), use the following:
90+
91+
```c#
92+
var builder = WebApplication.CreateBuilder(args);
93+
builder.Services.AddDbContext<MyContext>(options =>
94+
options.UseSqlServer(builder.Configuration.GetConnectionString("MyContext")));
95+
```
96+
97+
> [!NOTE]
98+
> <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseAzureSynapse*> was introduced in EF Core 9.0. When using an older version, use <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer*> instead.
99+
100+
***
101+
102+
## Compatibility level
103+
104+
You can optionally configure EF with the compatibility level of your database; higher compatibility levels allow for newer features, and configuring EF accordingly makes it use those features. If you do not explicitly configure a compatibility level, a reasonable default will be chosen that may not take advantage of the newest features. As a result, it's recommended to explicitly configure the compatibility level you'd like to have.
105+
106+
Note that this only covers EF's own configuration of the compatibility level - affecting e.g. the SQL it generates - but does not affect the compatibility level configured in your actual database. Databases hosted on newer versions of SQL Server may still be configured with lower compatibility levels, causing them to not support the latest features - so you may need to change the compatibility level in your database as well. For more information on compatibility levels, [see the documentation](/sql/relational-databases/databases/view-or-change-the-compatibility-level-of-a-database).
107+
108+
To configure EF with a compatibility level, use `UseCompatibilityLevel()` as follows:
109+
110+
```c#
111+
optionsBuilder.UseSqlServer("<CONNECTION STRING>", o => o.UseCompatibilityLevel());
112+
```
113+
114+
## Connection resiliency
115+
116+
EF includes functionality for automatically retrying failed database commands; for more information, [see the documentation](xref:core/miscellaneous/connection-resiliency). When using <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseAzureSql*> and <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseAzureSynapse*>, connection resiliency is automatically set up with the appropriate settings specific for those databases. Otherwise, when using <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer*>, configure the provider with <xref:Microsoft.EntityFrameworkCore.Infrastructure.SqlEngineDbContextOptionsBuilder.EnableRetryOnFailure*> as shown in the connection resiliency documentation.
117+
118+
In some cases, <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer*> may be called in code that you cannot control. Starting with EF 9, to enable connection resiliency in such scenarios, call `ConfigureSqlEngine(c => c.EnableRetryOnFailureByDefault())` beforehand (this is not necessary with <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseAzureSql*> and <xref:Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseAzureSynapse*>).
45119

46-
### EnableRetryOnFailureByDefault
120+
## Notes and caveats
47121

48-
EF 9 introduced `EnableRetryOnFailureByDefault` method that configures the context to default [execution strategy](xref:core/miscellaneous/connection-resiliency) unless it is configured explicitly (i.e. when using DI). To use it, simply call `ConfigureSqlEngine(c => c.EnableRetryOnFailureByDefault())` and later you can use `UseSqlServer` as usual. It is not necessary to call `EnableRetryOnFailureByDefault` when using `UseAzureSql` or `UseAzureSynapse`, as they already enable the default execution strategy.
122+
* The Microsoft.Data.SqlClient package ships more frequently than the EF Core provider. If you would like to take advantage of new features and bug fixes, you can add a direct package reference to the latest version of Microsoft.Data.SqlClient.
123+
* The EF SQL Server provider uses Microsoft.Data.SqlClient, and not the older System.Data.Client; if your project takes a direct dependency on SqlClient, make sure it references the Microsoft.Data.SqlClient package. For more information on the differences between Microsoft.Data.SqlClient and System.Data.SqlClient, [see this blog post](https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient).
49124

50-
## Supported Database Engines
125+
## Supported database engines
51126

52-
* Microsoft SQL Server (2012 onwards)
53-
* Azure SQL
127+
* Microsoft SQL Server (2019 onwards)
128+
* Azure SQL Database
54129
* Azure Synapse Analytics

entity-framework/core/providers/sql-server/misc.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ uid: core/providers/sql-server/misc
77
---
88
# Miscellaneous notes for SQL Server
99

10+
## Azure SQL database options
11+
12+
> [!NOTE]
13+
> You should [use `UseAzureSql` method](xref:core/providers/sql-server/index#usage) instead of `UseSqlServer` when connecting to Azure SQL.
14+
15+
Azure SQL Database provides [a variety of pricing options](https://azure.microsoft.com/pricing/details/sql-database/single/) that are usually configured through the Azure Portal. However, if you are managing the schema using [EF Core migrations](xref:core/managing-schemas/migrations/index), you can configure the desired options with EF, and they will get applied when EF creates the database.
16+
17+
You can specify the service tier of the database (EDITION) using [HasServiceTier](/dotnet/api/Microsoft.EntityFrameworkCore.SqlServerModelBuilderExtensions.HasServiceTier):
18+
19+
[!code-csharp[HasServiceTier](../../../../samples/core/SqlServer/AzureDatabase/AzureSqlContext.cs?name=HasServiceTier)]
20+
21+
You can specify the maximum size of the database using [HasDatabaseMaxSize](/dotnet/api/Microsoft.EntityFrameworkCore.SqlServerModelBuilderExtensions.HasDatabaseMaxSize):
22+
23+
[!code-csharp[HasDatabaseMaxSize](../../../../samples/core/SqlServer/AzureDatabase/AzureSqlContext.cs?name=HasDatabaseMaxSize)]
24+
25+
You can specify the performance level of the database (SERVICE_OBJECTIVE) using [HasPerformanceLevel](/dotnet/api/Microsoft.EntityFrameworkCore.SqlServerModelBuilderExtensions.HasPerformanceLevel):
26+
27+
[!code-csharp[HasPerformanceLevel](../../../../samples/core/SqlServer/AzureDatabase/AzureSqlContext.cs?name=HasPerformanceLevel)]
28+
29+
Use [HasPerformanceLevelSql](/dotnet/api/Microsoft.EntityFrameworkCore.SqlServerModelBuilderExtensions.HasPerformanceLevelSql) to configure the elastic pool, since the value is not a string literal:
30+
31+
[!code-csharp[HasPerformanceLevel](../../../../samples/core/SqlServer/AzureDatabase/AzureSqlContext.cs?name=HasPerformanceLevelSql)]
32+
33+
> [!TIP]
34+
> You can find all the supported values in the [ALTER DATABASE documentation](/sql/t-sql/statements/alter-database-transact-sql?view=azuresqldb-current&preserve-view=true).
35+
1036
## SaveChanges, database triggers and unsupported computed columns
1137

1238
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, or certain kinds of computed columns. For more information on this SQL Server limitation, see the documentation on the [OUTPUT clause](/sql/t-sql/queries/output-clause-transact-sql#remarks).

entity-framework/toc.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,25 +394,23 @@
394394
items:
395395
- name: Overview
396396
href: core/providers/sql-server/index.md
397-
- name: Temporal tables
398-
href: core/providers/sql-server/temporal-tables.md
399-
- name: Value generation
400-
href: core/providers/sql-server/value-generation.md
401397
- name: Function mappings
402398
href: core/providers/sql-server/functions.md
403399
- name: Columns
404400
href: core/providers/sql-server/columns.md
405401
- name: Indexes
406402
href: core/providers/sql-server/indexes.md
403+
- name: Value generation
404+
href: core/providers/sql-server/value-generation.md
405+
- name: Temporal tables
406+
href: core/providers/sql-server/temporal-tables.md
407407
- name: Memory-optimized tables
408408
href: core/providers/sql-server/memory-optimized-tables.md
409409
- name: Hierarchical data
410410
href: core/providers/sql-server/hierarchyid.md
411411
- name: Spatial data
412412
displayName: GIS
413413
href: core/providers/sql-server/spatial.md
414-
- name: Specify Azure SQL Database options
415-
href: core/providers/sql-server/azure-sql-database.md
416414
- name: Miscellaneous
417415
href: core/providers/sql-server/misc.md
418416
- name: SQLite

0 commit comments

Comments
 (0)