Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching from EF Automatic Migration to Code-First-Migration

I am currently attempting to switch from Entity Framework Automatic Migrations to Code-First Migrations. For a little backstory, this solution is split into four seperate projects: Data, Models, Services, and Web. The connection string information is located in the Web project, and the context is located in the data project.

Now, I have ran "Enable-Migrations", and that appears to have worked correctly.

From there, I delete the existing migrationHistory table in the database.

Running

Add-Migration -projectName Data" 

will generate an appropriate migration.

The Problem is when I attempt to run Update-Database, it results in a generic error:

> A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or
> was not accessible. Verify that the instance name is correct and that
> SQL Server is configured to allow remote connections. (provider: SQL
> Network Interfaces, error: 26 - Error Locating Server/Instance
> Specified)

I can connect to the database using the credentials supplied in the web.config, and the database connections have worked appropriately for the past several months, so I do not believe it is a connectivity problem unless Code-First migrations requires a different port than Automatic Migration.

I assumed that it was just not seeing the connection string supplied in the web.config, however running

Update-Database -projectName data -startupprojectname web

results in the same error

My question:

How do I get around the generic network-related error, given the above information? Is this problem a visiblity problem, in which the data project code-first migrations cannot see the connection string in the web project?

like image 696
Nathan Lafferty Avatar asked Nov 24 '14 17:11

Nathan Lafferty


People also ask

What is the difference between automatic migration VS code base migration?

There are those of us that think that "code migration" means "convert code from one language/platform to another" and that "automatic migration" means "do code migration automatically".

How do I revert my EF core migration?

To revert the last applied migration you should (package manager console commands): Revert migration from database: PM> Update-Database <prior-migration-name> Remove migration file from project (or it will be reapplied again on next step) Update model snapshot: PM> Remove-Migration.


1 Answers

Code first migrations - what connection string will it use?

Specifying the connectionStringName in the Update-Database / Add-Migration worked properly.

Update-Database -projectName Data -connectionStringName MyConnectionString
like image 64
Nathan Lafferty Avatar answered Oct 19 '22 11:10

Nathan Lafferty