entity framework - Traditional SqlClient can find server instance but EF can't -


I have a problem that I'm dashing my head for some time and I'm hoping someone's Knowing more about I can answer.

One part of my application uses the traditional database. The data is used to connect SQL Database to a database and the other uses the unit framework, so that the same SQL Server can connect to another database using these two connection strings:

  var connectionString = "Server = server \ SQLEXPRESS; database = database1; user = myUser; password = myPass;"; Var efConnectionString = "server = server \ SQLEXPRESS; database = database2; user = myUser; password = myPass;";   

I can fix the database with the non-EF part of my application. But when I try to do anything with EF part, I get an exception in saying that he can not find an instance of SQL Server:

System.Data.SqlClient.SqlException ( 0x80131904): A network-related or instance-specific error occurred while establishing a connection to the SQL Server server that was not found or was not accessible. Verify that the example name is correct and SQL Server is configured to allow remote connection. (Provider: SQL Network Interface, Error: 26 - Error specifying server / instance)

I have tripled that there is no typo in the EF connection string and even some colleagues have re-examined.

To ensure that it does not really make any sense, I have also checked the property context.Database.Connection.ConnectionString .

Update

My reference looks like this:

  Public class MyContext: DbContext {public MyContext () {} Public MyContext (string connString) : ConnString {} ...}   

I always build using an overloaded manufacturer that accepts a connection string:

  var reference = New MyContext (efConnectionString);   

I also removed the default constructor and created the project to ensure that it is not being used anywhere.

Finally found a problem!

I am using the first code in this application. In the startup code I set the startter for a new MigrateDatabaseToLatestVersion Starter:

  Database.SetInitializer & LT; MyContext & gt; (New MigrateDatabaseToLatestVersion & lt; MyContext, Migrations.Configuration & gt; ());   

When the database is initialized, it then starts using a new MyContext default constructor. Therefore it is trying to connect to the default SQL instance (either LocalBD or SQL Express) which is not present on my server.

If I want to use context with the right connection string, then I can tell it in the Manufacturer of MigrateDatabaseToLatestVersion :

  Database. SetInitializer & LT; MyContext & gt; (New MigrateDatabaseToLatestVersion & lt; MyContext, Migrations.Configuration & gt; (useSuppliedContext: True));   

I think it should be the default behavior. The beginner needs to make a new reference, when it is supplied after running it? Oh fine.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -