传统的SqlClient可以找到服务器实例,但EF不能 [英] Traditional SqlClient can find server instance but EF can't

查看:115
本文介绍了传统的SqlClient可以找到服务器实例,但EF不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我一直在跳动一段时间,我希望有一个比我更了解的人可以回答。



我的应用程序的一部分使用传统的System.Data.SqlClient东西连接到一个数据库,另一部分使用实体框架连接到同一SQL服务器上使用这两个连接字符串的不同数据库:

  var connectionString = 
Server = SERVER\SQLEXPRESS; Database = Database1; User = myUser; Password = myPass;;

var efConnectionString =
Server = SERVER\SQLEXPRESS; Database = Database2; User = myUser; Password = myPass;;

使用我的应用程序的非EF部分,我可以打数据库。但是当我尝试用EF部分做任何事情时,我得到一个例外,说它找不到SQL Server实例:


系统。 Data.SqlClient.SqlException(0x80131904):与SQL Server建立连接时发生与网络相关或实例特定的错误。服务器未找到或无法访问。验证实例名称是否正确,并将SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:26 - 定位服务器/实例指定错误)


我已经三重检查EF连接字符串中没有打字错误,甚至有几个同事重新检查它。我还检查了 context.Database.Connection.ConnectionString 属性,以确保它正在使用正确的连接字符串,它是。


$ b $这真的没有任何意义。什么会导致EF找不到SQL Server,当我尝试过其他方法可以吗?



更新



我的上下文看起来像这样:

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

我总是使用接受连接字符串的重载构造函数构造:

  var context = new MyContext(efConnectionString); 

我甚至删除了默认构造函数,并构建了项目,以确保它在任何地方都不被使用

解决方案

最后找到问题!



代码首先在这个应用程序。在启动代码中,我将初始化程序设置为一个新的 MigrateDatabaseToLatestVersion 初始化程序:

  Database.SetInitializer< MyContext>(
new MigrateDatabaseToLatestVersion< MyContext,Migrations.Configuration>());

当数据库初始化时,会实例化一个新的 MyContext 使用默认构造函数。因此,它尝试连接到我的服务器上不存在的默认SQL实例(LocalDb或SQLExpress)。



如果我想使用正确的上下文连接字符串我可以在 MigrateDatabaseToLatestVersion 的构造函数中指出:

  Database.SetInitializer< MyContext>(
new MigrateDatabaseToLatestVersion< MyContext,Migrations.Configuration>(
useSuppliedContext:true));

这对我来说似乎应该是默认行为。为什么初始化程序需要创建一个新的上下文,如果它运行时提供一个?哦,好的。


I'm having a problem that I've been beating my head over for a while now and I'm hoping someone who knows more about this than I do can answer it.

Part of my application uses the traditional System.Data.SqlClient stuff to connect to one database and another part uses Entity Framework to connect to a different database on the same SQL server 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;";

With the non-EF part of my application I can hit the database just fine. But when I try to do anything with the EF part I get an exception saying it can't find the SQL Server instance:

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)

I've triple-checked that there are no typos in the EF connection string and even had a couple co-workers re-check it. I also checked the context.Database.Connection.ConnectionString property to make sure it was using the right connection string and it is.

This really doesn't making any sense. What would cause EF to not find the SQL Server when every other method I've tried can?

Update

My context looks like this:

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

I always construct using the overloaded constructor that accepts a connection string:

var context = new MyContext(efConnectionString);

I even removed the default constructor and built the project just to make sure it wasn't being used anywhere else.

解决方案

Finally found the problem!

I am using code first in this application. In the startup code I set the initializer to a new MigrateDatabaseToLatestVersion initializer:

Database.SetInitializer<MyContext>(
    new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());

When the database is initialized it instantiates a new MyContext using the default constructor. So it's trying to connect to the default SQL instance (either LocalDb or SQLExpress) which doesn't exist on my server.

If I want to use the context with the correct connection string I can tell it to in MigrateDatabaseToLatestVersion's constructor:

Database.SetInitializer<MyContext>(
    new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>(
        useSuppliedContext: true));

This to me seems like it should be the default behavior. Why would the initializer need to create a new context if it is supplied one when it's run? Oh well.

这篇关于传统的SqlClient可以找到服务器实例,但EF不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆