Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type or namespace name 'MySQL' could not be found VS2017

Tags:

c#

mysql

Currently trying to create a connection to a MySql Database. When I added

using MySql.Data; using MySql.Data.MySqlClient;

to my class I recieved the typical "The type or namespace name ... could not be found". A little searching made me realize I was missing the MySql.Data.dll.

I used Packet Manager console to install MySql.Data:

PM> Install-Package MySql.Data

I verified that it was installed via Tools > NuGet Package Manager > Manage NuGet Packages for Solutions, but I was still getting the build error.

At this point I figured, since the PM downloaded the .dll I'll just manually reference it via References > Add Reference > Browse > Select MySql.Data.dll.

Great!! The red squiggly went away on my using and intellisense started working for classes inside the MySql.Data namespace:

enter image description here

Fast forward 10 mins, I finish writing my test class and hit Build and error comes back:

enter image description here

So in summary, using PM to install MySql.Data got me no where. Manually adding the reference of MySql.Data.dll got my intellisense to recognize the namespace and classes, but when it comes to building I still get the error.

Pretty stumped. Anyone seen this?

like image 549
Flippi Avatar asked Nov 08 '17 05:11

Flippi


2 Answers

Solved this issue! Turns out the newest MySql.Data.dll is built off of .Net Framework 4.5.2, while my project was using .Net Framework 3.5 Client Profile.

Changing my project's framework to 4.5.2 solved the issue. It's quite strange that VS doesn't give a better error message.

To change your project's framework right click on your project (not the solution), select Properties -> Application -> Target framework. The target framework is a dropdown on that page.

I came to this realization thanks to another stackoverflower question: Namespace not recognized (even though it is there)

like image 73
Flippi Avatar answered Oct 21 '22 14:10

Flippi


In terminal after navigating to your project directory, run the following commands

dotnet add package MySql.Data -v 8.0
like image 5
mruanova Avatar answered Oct 21 '22 14:10

mruanova