When I try to connect to the MySql database using LINQ's DataContext and a connection string, it fails at runtime due to not being able to find the network path. This used to work when I was connecting to a MS SQL database. It fails at runtime with the exception:
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
Failing code:
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();
When I use MySqlConnection directly, it works, so I know my connection string and server configuration are good:
MySqlConnection conn = new MySqlConnection(connectionString);
conn.Open();
conn.Close();
return "Test successful.";
The connection string looks like this:
Database={database_name};Data Source={webhost.net};User Id={mysql_user};Password={password}
In my .csproj I'm using the following references as part of the MySql Connector 6.9.9 installer:
MySql.Data
MySql.Data.Entity.EF6
MySql.Web
I suspect that LINQ doesn't know I'm trying to connect to a MySql database and is trying to use the connection string as it were for a MS SQL database. Is there a way to use LINQ's DataContext with a MySql database?
