I have an ASP.NET Core Web API project referencing an EF Core project.
When I scaffolded the EF Core project, it automatically set up the connection string within the OnConfiguring method:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=etc;Database=etc;uid;pwd;");
}
}
Then I found out that the equivalent of placing the connection string in a web.config is to place it within an appSettings.json file of the Web API project, and read it in the ConfigureServices method of the Startup class of the Web API project etc...
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c => ...);
var CNN_STR = Configuration.GetConnectionString("DBNameInAppSettingsJson");
services.AddDbContext<MyDBContext>(options =>
options.UseSqlServer(CNN_STR));
}
But now the connection string is in two places! Am I supposed to remove it from the OnConfiguring() method of the EF Core project? If so, how?