Gratzy
Gratzy

Reputation: 2908

Code first update-database uses localdb connection string which is not specified

Visual Studio 2019, C#

I'm working on my first code first implementation.

I built out a data access layer which works great. I then went to add/test out a data migration.

I created a migration file then went to the package manager console and ran this:

update-database

after a long pause I saw a message that stated:

Target database is: 'OMDB' (DataSource: (localdb)\mssqllocaldb, Provider: System.Data.SqlClient, Origin: Convention).

OMDB is my model. However, I'm not pointing it to localDB in my app.config, I'm pointing to sqlexpress:

….
<connectionStrings>
<add name="OMDB" connectionString="data source=.\SQLEXPRESS;initial catalog=MyDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value ="data source=.\SQLEXPRESS;initial catalog=MyDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"/>
  </parameters>
  </defaultConnectionFactory>
  <providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
   </providers>

…

In order to get the data access layer built out, I had to add an override to the class constructor to load the connection string:

    public OMDB(string szConnectionString) : base(szConnectionString)
    { }

But I don't think that has anything to do with my issue...

I have the default project set to the data access layer project (DAL), I also tried forcing that on the update-database line:

update-database -Verbose -ProjectName DAL

I did a global search for localDB in my solution and it came up with no hits so I have no idea where it's getting the idea of it using the localdb for the source...?

Any suggestions on where it's pulling up the localDB for this connection?

Upvotes: 2

Views: 1643

Answers (2)

Dash
Dash

Reputation: 834

I managed to fix the issue by following steps.

  1. Set your website project(where the web.config has the connectionStrings) as Default/Startup project in Visual Studio
  2. Make sure the connectionString points to the actual db instance
  3. Run Enable-Migrations or Enable-Migrations -Force in Package Manager Console depending on your need

You should get a message like below upon successful creation of the InitialModel:

Detected database created with a database initializer. Scaffolded migration '202102050903200_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter.

Upvotes: 2

Gratzy
Gratzy

Reputation: 2908

Ok, this one really made me burn some time.... I figured out what was causing it and also a workaround at least for now.

I have a project in the solution which is my data access layer. Even though I had that project selected in the package manager console, it created a blank app config at the top level. Since that app.config did not have the connection string tags in it, it was defaulting to localdb and eventually erroring out.

So to get around that, I copied the connection string from the data access library project app.config to the master project app.config and everything started working. Not sure why it wanted to go to the main project file but it was... I'll have to come back to this later but it's working for now.

Upvotes: 0

Related Questions