this is my appsettings.json
"ConnectionStrings": {
"Circolari": "Server=abcde;Database=Circolari;Trusted_Connection=True;"
}
and this is my startup.cs
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = configurationBuilder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//Circolari
string connectoionString = Configuration.GetConnectionString("Circolari");
services.AddDbContext<CircolariContext>(options => options.UseSqlServer(connectoionString));
services.AddMvc();
}
so when i do a query
using (CircolariContext db = new CircolariContext(new DbContextOptions<CircolariContext>()))
{
List<Argomenti> listaArgomenti = db.Argomenti.ToList();
}
i have the problem: An unhandled exception occurred while processing the request.
InvalidOperationException: A named connection string was used, but the name 'Circolari' 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.
Microsoft.EntityFrameworkCore.Storage.Internal.NamedConnectionStringResolverBase.ResolveConnectionString(string connectionString)
could someone help me please?
appSettings.json
is being loaded? You've marked it as optional, so ifenv.ContentRootPath
isn't the right folder, it won't be loaded. Try marking it asoptional: false
to see if it throws an exception. – Surreyoptional: false
. – Humoristvar connectoionString = Configuration.GetValue<string>("ConnectionStrings:Circolari");
is another way to get from configuration – Humoristnew CircolariContext(new DbContextOptions<CircolariContext>())
, it doesn't use your configuration – Houseraising