I am making changes to my database requiring EF migrations for the first time in years. This means that there have been lots of code base changes over the past 2 years, but this is the first time my model has changed for ages. I am using EF 6.1.3, VS 2017 and SQL Express installed on a server on the LAN.
I have EF migrations enabled on the project and am able to "Add-Migration". When it comes to "Update-Database", however, I have the following 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 am aware that this sort of thing can happen when TCP connections have not been enabled, but I've double checked this and am able to connect to the database with no problems using the same DbContext object and connection string from my application. I can also use SQL Server Object Explorer. It's only Update-Database that fails. I'm on a domain and am using domain-level authentication.
Is there some other permission I need to enable to allow EF Migrations to work?
EDIT: For those who have suggested things like checking the startup project in the comments below, I am providing the start of the error trace. I have not provided the entire trace as that takes us through the whole Migrations Stack of function names. The relevant part including my exact command line is:
PM> Update-Database -ProjectName "AJSoft.CN2.Data.Model" -Verbose Using StartUp project 'AJSoft.CN2.Data.Model'. Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Target database is: 'CN2 Test' (DataSource: SERVER\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: UserCode). System.Data.SqlClient.SqlException (0x80131904): 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) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
The startup project is the project with access to the connection string and my result is the same whether I specify the "Model" project above or my actual WPF startup project.
OK - for anyone else looking at this, I have solved the issue.
On further thinking about this, there had to be a different connection string used for my project and for EF migrations, otherwise the message makes no sense.
The connection string provided to EF Migrations is set up in a static constructor to my custom DbContext class. As far as I was concerned, this was correct behaviour. However when the project is compiled and run, a settings file is read and my code adds some other information to the connection string.
That is why there was a discrepancy between the two - adding the information to the CS in the static constructor fixed the error.
My problem was that I had different environment (ASPNETCORE_ENVIRONMENT) set up for Package Manager Console than for my .NET Core project.
Get-ChildItem Env: in Package Manager Console to list your environment variables.
Update-Database -Verbose to see which environment is used by the command.
Than I changed my environment variable, restarted Visual studio and it used the correct project settings.