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

Fix sample code for connection strings from Configuration #3877

Open
the-programmer opened this issue May 27, 2022 · 0 comments
Open

Fix sample code for connection strings from Configuration #3877

the-programmer opened this issue May 27, 2022 · 0 comments

Comments

@the-programmer
Copy link

the-programmer commented May 27, 2022

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:

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BloggingContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}

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.

builder.Services.AddDbContext<AppDbContext>(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

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ajcvickers ajcvickers added this to the 7.0.0 milestone May 31, 2022
@ajcvickers ajcvickers self-assigned this May 31, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
@AndriySvyryd AndriySvyryd changed the title Add sample code for connection strings in .NET 6 Fix sample code for connection strings from Configuration Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants