1

I'm developing an ASP.NET Core application and decided to introduce EF Core with code-first migrations. I created a DbContext class, and registered it in Startup class. For connection string I've opened SQL Server Object Explorer in VS2019, right clicked on (localdb)/MSSQLLocalDB and copy-pasted the connection string from Properties window, which looked like this:

Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False.

The update-database command from package manager console worked fine, with no errors however I couldn't find any CREATE DATABASE statement from the SQL generated. Now I technically have a database, but I can't see it anywhere in SQL Server Object Explorer nor SQL Server Management Studio.

When I changed the connection string to something like this "server=(localdb)\\mssqllocaldb; database=QuizNet;Trusted_Connection=True;MultipleActiveResultSets=true" I get an error:

Cannot create file 'C:\Users\ACCOUNT_NAME\QuizNet.mdf' because it already exists. Change the file path or the file name, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

After changing the database name to i.e. QuizNet1, like this "server=(localdb)\\mssqllocaldb; database=QuizNet1;Trusted_Connection=True;MultipleActiveResultSets=true" it does indeed create a database and I CAN see it in SQL Server Object Explorer.

So it does look like I've messed something with connection string in my first attempt and EF Core created a database, but I can't find it anywhere.

Another minor issue - How does EF Core know the connection string when calling update-database? The connection string is set in ConfigureServices method and - as far as I know - this method is called at runtime, not compile-time.

Chris
  • 41
  • 2
  • 6
  • Check the installation folder of Microsoft SQL Server "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA" and you should see an .mdf and .ldf file for QuizNet. Remove them and your database and logs will be deleted. – Azhar Khorasany Dec 09 '19 at 15:41
  • https://stackoverflow.com/questions/33911316/entity-framework-core-how-to-check-if-database-exists – Mrg Gek Dec 09 '19 at 15:44

1 Answers1

0

EF Core, when included in an ASP.NET Core project will use Startup to bootstrap itself. If it's instead included via a class library (i.e. something that cannot be run directly), then you either must create an IDesignTimeDbContextFactory implementation, which defines the connection string, or override OnConfiguring in the context to define the connection string there. In both of the latter cases, the connection string would be for development purposes only. Neither method should be used for production workflows.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • "Neither method should be used for production workflows." - You mean hard-coding the connection string directly in code or including EF in class library and using one of the methods you described? – Chris Dec 09 '19 at 16:33