环境:.NET Framework 4.6,VS 2015,Entity Framework 6.x
我正在尝试连接到远程服务器但是当我尝试从Entity Framework连接时出现此错误:
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)
InnerException = {“找不到网络路径”}
我能够从SQL Server Management Studio连接到同一台服务器。
请指点。
MultipleTestModel.Context.cs
public partial class fccidevEntities : DbContext
{
public fccidevEntities()
: base(hr.common.Database.EntitiesConnectionString("res://*/ef.MultipleTestModel.csdl|res://*/ef.MultipleTestModel.ssdl|res://*/ef.MultipleTestModel.msl"))
{
}
...
}
common.Database.EntitiesConnectionString:
public static string EntitiesConnectionString(string model,string)
{
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["fccidevConnection"].ConnectionString);
builder["MultipleActiveResultSets"] = true;
builder["Connect Timeout"] = 30;
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = builder.ConnectionString;
entityBuilder.Metadata = model;
return entityBuilder.ToString();
}
catch(Exception ex)
{
throw ex;
}
}
DAL:
using (var dbTest = new fccidevEntities())
{
var EmployeeInformation = await dbTest.Employees.Where(x => x.Id == 10).FirstOrDefaultAsync();
}
web.config:
<add name="fccidevConnection"
connectionString="Data Source=System.Data.SqlClient;Initial Catalog=dev.ca.atech.com;Integrated Security=False;User Id=sa;Password=*****;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
答案 0 :(得分:5)
配置文件中的连接字符串出错。
此:
Data Source=System.Data.SqlClient
......不正确。它应该是这样的:
Data Source=SERVER_HOST_NAME_OR_IP\SQL_SERVER_INSTANCE_NAME
几个例子:
Data Source=HP-14\SQLEXPRESS
Data Source=.\SQLEXPRESS
Data Source=192.168.0.19\INSTANCE14
答案 1 :(得分:0)
需要将服务器配置为处理来自同一登录的多个连接。或者您必须以这样一种方式管理您的连接:您只能随时打开一个连接