I have a ASP NET CORE project, where I have a connection string and my dbcontext is in a .net 4.52 class library project. I would like to know how to access the connection string from the asp net core project.
asp net core - appsettings.json:
"ConnectionStrings": {
"MyConnectionString": "Data Source=.\\mydb;Initial Catalog=mydb;User Id=myuser;Password=mypassword; MultipleActiveResultSets=true"
}, ...
My dbContext
:
public MyContext() : base("name=MyConnectionString")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());
}
public MyContext(string connString) : base(connString)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());
}
When I run the app I get the error: no connection string named MyConnectionString
could be found in the application config file. Even if I add a connection string section to the asp net core project web.config file.
When I hardcode the connectionstring everything works fine.