0

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?

2
  • Your DbContext does not know which provider to use. You need to update config file with proper provider name. Try this - dev.mysql.com/doc/connector-net/en/… Commented Apr 4, 2017 at 5:27
  • Can you post your entire connection string xml (with provider name) ? Commented Apr 4, 2017 at 5:54

1 Answer 1

1

You are connecting MySql database and error genrate from "System.Data.SqlClient.SqlException"that means it could not connect MySql detabase and try to connect with Sql database.

Try to add MySql database by DataConnection in Server Explorer enter image description here

If u will not get this option then you have to install "Visual studio MySql Connector" After successfull install restart your VS.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, I don't have the option to choose a MySQL database. The Visual Studio MySQL Connector doesn't currently support VS 2017, but the next version will: forums.mysql.com/read.php?174,656341,656363#msg-656363

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.