Error in update-database command in code first migration

c# code-migration entity-framework sql-server

Question

I am working on Desktop application in WPF and creating SqlRepository with LocalDB to store data. I am using following tools

  • Visual Studio 2013 Community with update 2
  • Entity Framework 6.1.2 for Code first migration

I have created local database with Microsoft SQL Server Database File (SqlClient) configuration. Database created successfully and below is connection string fetched from Sql

Data Source=(LocalDB)\v11.0;Initial Catalog=D:\USERS\USERNAME\DOCUMENTS\TestDatabase\testdb.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False

I am using same connection string in app.config. I can view database object in SQL Server Database explorer in Visual Studio. But when run update-database command in Nuget package manager console, I am getting below 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)

1
21
1/4/2015 5:25:46 PM

Accepted Answer

I had similar problem and fixed it when I changed the "start-up project" from another module to the module containing references to all other projects in the solution. Right-click module >> click "Set as StartUp Project"

62
1/24/2015 10:54:44 PM

Popular Answer

Diego's answer is correct.

This problem occurs when there is no connection string in project marked as startup project. Then EF tries to connect to some default database engine to perform update. In my case it tried to use express, and for some reason it couldn't connect. And the error was thrown.

Run your "update-database" with option "-Verbose". One of the lines there shows which StartUp project is used. Check your connection string in this project, or change the startup project to the one that has correct connection string. That solves the problem.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow