User475983607 posted
luka ogadze
i scaffolded data with ef core in class library. dbcontext class includes hardcoded connection string. is there any beter way to pass string in dbcontext class?
The Getting Started EF Core reference docs explains how to scaffold a dbcontext and move the connection string to DI.
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
Next move the connection string to appsetting,json and use configuration to fetch the connection string.
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
string connectionstring = Configuration.GetConnectionString("DefaultConnection");
Configuration reference.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1&tabs=basicconfiguration