dotnet / dotnet/EntityFramework.Docs
Fix sample code for connection strings from Configuration
- Dominant language
- Mermaid
- Stars
- 1.7k
- Forks
- 2k
- Avg merge
- 7d 23h
- Merged PRs (30d)
- 16
Description
Today i wanted to start with a simple blazor app. However it seems that the syntax for loading the connection string has been changed in .NET 6.
The current way is:
https://github.com/dotnet/EntityFramework.Docs/blob/7e13def829830a9f2ffa173f92c7e3eb94debef5/entity-framework/core/miscellaneous/connection-strings.md?plain=1#L36-L40
However this seems to generate a CS0103 `The name 'Configuration' does not exist in the current context`. The namespace `Microsoft.Extensions.Configuration` is imported.
In .NET 6 the code seems to be the following instead of the current sample. However this generates the error.
```c#
builder.Services.AddDbContext(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase"));
});
```
Steps to reproduce.
- In VS2022 create a new "Blazor server app" or "Blazor webassembly app" (when creating a "Blazor webassembly app" be shure to select the "ASP.NET Core hosted" checkbox)
- Set the framework to .NET 6.0
- Open "Program.cs"
- Try to set the connection string.
Update: If you use the "Individual accounts" template the correct syntax is generated
```c#
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext(options =>
options.UseSqlServer(connectionString));
```
---
#### Document Details
⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*
* ID: de9db02d-7310-1141-6aeb-539c02326a36
* Version Independent ID: 90062036-aa01-3f73-dac9-b4a092245721
* Content: [Connection Strings - EF Core](https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings)
* Content Source: [entity-framework/core/miscellaneous/connection-strings.md](https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/miscellaneous/connection-strings.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @bricelam
* Microsoft Alias: **avickers**
Contributor guide
Assessment
This issue has not been assessed yet.