2016-05-20 45 views
1

環境:.NET Framework 4.6,VS 2015,實體框架6.x未找到網絡路徑。提供者:命名管道提供程序,錯誤:40 - 無法打開連接到SQL Server

我試圖連接到遠程服務器,但我當我嘗試從實體框架連接時出現此錯誤:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

InnerException = {"The network path was not found"}

我能夠從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" /> 

回答

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 
0

需要將服務器配置爲處理來自同一登錄的多個連接。或者您必須以這樣的方式管理您的連接,以至於您只有一個連接隨時打開:

相關問題