2017-04-04 56 views
0

當我嘗試使用LINQ的DataContext和連接字符串連接到MySql數據庫時,由於無法找到網絡路徑,它在運行時失敗。當我連接到MS SQL數據庫時,這用於工作。它無法在與異常運行時:在ASP.NET 4.5中使用LINQ和MySql

System.Data.SqlClient.SqlException: '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)' 

Inner Exception 
Win32Exception: The network path was not found 

失敗代碼:

DataContext db = new DataContext(connectionString); 
Table<FlightPriceModel> flightsTable = db.GetTable<FlightPriceModel>(); 
IQueryable<FlightPriceModel> query = from row in flightsTable select row; 
return "Number of rows in FlightPrices table: " + query.Count(); 

當我直接使用的MySqlConnection,它的作品,所以我知道我的連接字符串和服務器配置都不錯:

MySqlConnection conn = new MySqlConnection(connectionString); 
conn.Open(); 
conn.Close(); 
return "Test successful."; 

連接字符串看起來像這樣:

Database={database_name};Data Source={webhost.net};User Id={mysql_user};Password={password} 

在我的.csproj我使用以下引用作爲MySql Connector 6.9.9 installer的一部分:

MySql.Data 
MySql.Data.Entity.EF6 
MySql.Web 

我懷疑LINQ不知道我試圖連接到MySQL數據庫,並嘗試使用連接字符串,就像MS SQL數據庫一樣。有沒有辦法使用LINQ的DataContext與MySql數據庫?

+0

您的DbContext不知道使用哪個提供程序。你需要用適當的提供者名稱更新配置文件。試試這個 - https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html –

+0

你可以發佈你的整個連接字符串xml(提供者名稱)嗎? – Vandita

回答

1

您正在從「System.Data.SqlClient.SqlException」連接MySql數據庫和錯誤genrate,這意味着它無法連接MySql detabase並嘗試連接Sql數據庫。

嘗試通過DataConnection添加MySql數據庫服務器資源管理器enter image description here

如果妳不會得到這個選項,那麼你必須安裝「Visual Studio的MySQL連接」 後全成安裝重新啓動VS.

+0

感謝您的回覆,我沒有選擇MySQL數據庫的選項。 Visual Studio MySQL Connector目前不支持VS 2017,但下一個版本將會:https://forums.mysql.com/read.php?174,656341,656363#msg-656363 – pwnsauce