Named connection strings are only supported when using 'IConfiguration' and a service provider

See original GitHub issue

could you please tel me what i am doing wrong?

SCAFOLD DBCONTEXT

Scaffold-DbContext -Connection name=DbAsp Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f

CONFIGURESERVICES

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            
            services.AddDbContext<aspContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DbAsp")));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

ASPCONTEXT.CS

public partial class aspContext : DbContext
    {
        public aspContext()
        {
        }

        public aspContext(DbContextOptions<aspContext> options)
            : base(options)
        {
        }
		
		...
}

EXCEPTION

InvalidOperationException: A named connection string was used, but the name ‘DbAsp’ was not found in the application’s configuration. Note that named connection strings are only supported when using ‘IConfiguration’ and a service provider, such as in a typical ASP.NET Core application. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.

APPSETTINGS.JSON

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },

  "AllowedHosts": "*",

  "ConnectionStrings": {
    "DbAsp": "Server=xxx;Database=xyz;integrated security=True;"
  }
}

Exception message: Stack trace: A named connection string was used, but the name ‘DbAsp’ was not found in the application’s configuration. Note that named connection strings are only supported when using ‘IConfiguration’ and a service provider, such as in a typical ASP.NET Core application. See https://go.microsoft.com/fwlink/?linkid=850912 for more information

Further technical details

EF Core version: (found in project.csproj or packages.config) Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Operating system: IDE: (e.g. Visual Studio 15.7.2) .NET CORE 2.2 release 3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:26 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
mmu69commented, Jun 10, 2020

I had this problem with a new Core 3.1 project and a Scaffold-DbContext. Startup.cs has a IConfiguration. Adding the DdContext to startup.cs solved the problem

    public void ConfigureServices(IServiceCollection services)
    {
        ........
        services.AddDbContext<StipContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("StipDatabase")));
    }
2reactions
sibeliuzcommented, Nov 28, 2019

Still does not work in .net core 3.xx web api projects

Read more comments on GitHub >

github_iconTop Results From Across the Web

A named connection string was used, but the ...
Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.
Read more >
RelationalStrings.NamedConnectionStringNotFound ...
Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.NET Core application....
Read more >
Store and read connection string in appsettings.json
Different ways to store and read connection strings in appsettings.json. Learn some easy ways to work with connection strings in .NET Core.
Read more >
Not able to configure connection string in ASP.net MVC ...
A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider.
Read more >
Connection String in Entity Framework Core
Connection string contain information about the data source that is being connected. This information varies from provider to provider, but ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found