EF6 Problems Connecting for Update-Database Command

c# ef-migrations entity-framework-6 sql-server visual-studio

Question

For the first time in years, I'm making changes to my database that call for EF migrations. This indicates that even if the code base has undergone several changes over the last two years, my model hasn't changed in a long time. I have SQL Express installed on a LAN server along with EF 6.1.3, VS 2017, and other software.

I can "Add-Migration" since EF migrations are enabled on the project. However, when I try to "Update-Database," I get 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)

Using the same DbContext object and connection string from my application, I can connect to the database without any issues. I am aware that this kind of thing might occur when TCP connections are not allowed, but I have double tested this and am able to do so. Object Explorer for SQL Server is another option. Only Update-Database is ineffective. I'm using domain-level authentication and am connected to a domain.

Do I need to enable any additional permissions for EF Migrations to function?

EDIT: I'm included the beginning of the problem trail in response to suggestions made in the comments section below, including examining the starter project. I haven't included the complete trace since it goes through every function name in the Migrations Stack. The pertinent portion, which includes my precise 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)

Whether I provide the "Model" project mentioned above or my real WPF startup project, my results are the same since the startup project is the one having access to the connection string.

1
0
9/19/2017 3:31:36 PM

Accepted Answer

Okay, I think I've found the solution for anybody else looking at this.

After some consideration, I've concluded that a distinct connection string must be used for my project and for EF migrations in order for the message to make sense.

The static function Object() { [native code] } of my own DbContext class is where I put up the connection string that EF Migrations uses. I considered this to be appropriate behaviour. When a settings file is read and the project is built and launched, my code adds additional data to the connection string.

That is why there was a disparity between the two; the problem was corrected by adding the data to the CS in the static function Object() { [native code] }.

0
9/22/2017 11:07:45 AM

Popular Answer

My environment (ASPNETCORE ENVIRONMENT) was set up differently for Package Manager Console than it was for my.NET Core project, and this caused an issue.

Use Get-ChildItem Env: to list your environment variables in the Package Manager Console.

I usedUpdate-Database -Verbose to determine the command's environment.

When I reopened Visual Studio and adjusted my environment variable, it utilised the appropriate project settings.



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