I am having the following exception:
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "ScraperDB" requested by the login. The login failed.
Login failed for user 'myname'
I guess it's normal that the login fails but the DB hasn't been created yet!
What I do is:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.Build();
var connectionString = configuration["ConnectionString"];
EnsureDatabase.For.SqlDatabase(connectionString);
where my connection string is:
"ConnectionString": "Server=.;Database=ScraperDB;Integrated Security=True;"
after the exception is thrown and the console crashes, I check out the DB and it's created..
User contributions licensed under CC BY-SA 3.0