Named connection strings are only supported when using 'IConfiguration' and a service provider
See original GitHub issuecould 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:
- Created 5 years ago
- Comments:26 (5 by maintainers)
Top Related StackOverflow Question
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
Still does not work in .net core 3.xx web api projects