當我嘗試使用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數據庫?
您的DbContext不知道使用哪個提供程序。你需要用適當的提供者名稱更新配置文件。試試這個 - https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html –
你可以發佈你的整個連接字符串xml(提供者名稱)嗎? – Vandita