locked
Why does installing the MySql.Data.Entity package cause errors with MySqlConnection code? RRS feed

  • Question

  • User-696641838 posted

    I recently installed the MySql.Data.Entity package so that my Entity Framework code would work with a MySQL database instead of a previous SQL database.  I also changed all references of ‘SqlConnection’, ‘SqlCommand’, and ‘SqlParameter’ to ‘MySqlConnection’, ‘MySqlCommand’, and ‘MySqlParameter’.  This code was necessary because there was no Entity Framework code that would work with the reader and the removal of table rows.  But, errors were generated wherever these references are used, which state that these types exist.  What does this mean and what must I do to rectify these errors?  The following is an example of how these references are used in my C# website code to remove a row from a table:

     

    // Delete Profile table row

    MySqlConnection conn = null;

    conn = new MySqlConnection(EFMconn);

     

    MySqlCommand cmd = new MySqlCommand(

    "DELETE Profile WHERE MemberID = @OID", conn);

     

    MySqlParameter param1 = new MySqlParameter();

    param1.ParameterName = "@OID";

    param1.Value = OID;

     

    cmd.Parameters.Add(param1);

     

    conn.Open();

    int rowsAffected = cmd.ExecuteNonQuery();

     

    if (conn != null)

        conn.Close();

    Wednesday, September 16, 2015 5:14 PM

Answers

  • User-696641838 posted

    I fixed this problem by simply removing the duplicate ‘MySql.Data’ declaration in the web.config file.  I came to realize that the error message, which stated: The type 'MySqlConnection' exists in both 'MySql.Data’ versions, was referring to an extra declaration.  I simply commented out the declaration in my web.config file and the code compiled fine without errors.  I’m sorry to have troubled all of you about this.

     

    Maurice

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 19, 2015 1:24 PM

All replies

  • User603616845 posted

    Hi,

    Please refer these link. I am sure this will help to resolve this issue. 

    http://www.codeproject.com/Tips/423233/How-to-Connect-to-MySQL-Using-Csharp

    http://www.c-sharpcorner.com/UploadFile/47548d/simple-insert-select-update-and-delete-in-Asp-Net-using-mysq/

    Hope this will help you.

    thanks

    Thursday, September 17, 2015 1:44 AM
  • User269602965 posted

    The MySQL data entity documentation states other MySQL connection components and drivers must be installed as well.

    Thursday, September 17, 2015 12:40 PM
  • User-696641838 posted

    Thank you for responding with links to useful information about how to connect to MySQL using C#.  I examined both links very closely, but I fail to find where the problem is.  The first document provides 6 steps.  Step 1 says to open a web page in Visual Studio and create a MySql database for it to access.  I opened a page in an existing project and used MySQL Workbench to create a database.  Step 2 refers to the creation of a table within the database, which I also did with MySQL Workbench.  Step 3 says to add new references into the project of files MySql.dll and MySql.web.dll, which was done.  Step 4 states to add the namespace “using MySql.Data.MySqlClient;” to the project, which was also done.  Step 5 refers to creating a MySQL connection string, which was added immediately after the public partial class declaration for the web page.  The following connection string refers to the local database in my  computer.

     

      <connectionStrings>

        <add name="ENCdbConnectionString" connectionString="Data Source=.NET Framework Data Provider for MySQL;Initial Catalog=v1db;User id=Maurice;Password=123;Trusted_Connection=True"/>

      </connectionStrings>

     

    Step 6 specifies code that inserts data into a MySQL table.  The following is how it defines a MySqlConnection:

     

            #region MySqlConnection Connection and Page Lode
            MySqlConnection conn = new
    MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);       

    The following is how it is defined within the C# code of my web page:

        string EFMconn = ConfigurationManager.ConnectionStrings["ENCdbConnectionString"].ToString();

     

    MySqlConnection conn = null;

    conn = new MySqlConnection(EFMconn);

     

    Unfortunately, every time that MySqlConnection is used in my code, it generates an error Code: CS0433 with a description: The type 'MySqlConnection' exists in both 'MySql.Data, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' and 'MySql.Data, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'

     

    So, what am I doing wrong?

     

    Maurice

    Thursday, September 17, 2015 6:03 PM
  • User-696641838 posted

    PLEASE REMEMBER that all of this code was working properly without error UNTIL I installed the MySql.Data.Entity package in order to enable my Entity Framework code to ALSO work with the new MySql database.  Actually, the link provided to help me failed to mention the step to have Server Explorer connect to the MySql database, which tested successfully.  Now, it appears that there is a problem with having both ways to interact with the database.  I need both ways to work since Entity Framework does not include any way to remove unwanted rows from tables.  Please help.

     

    Maurice

    Friday, September 18, 2015 7:19 AM
  • User-696641838 posted

    I fixed this problem by simply removing the duplicate ‘MySql.Data’ declaration in the web.config file.  I came to realize that the error message, which stated: The type 'MySqlConnection' exists in both 'MySql.Data’ versions, was referring to an extra declaration.  I simply commented out the declaration in my web.config file and the code compiled fine without errors.  I’m sorry to have troubled all of you about this.

     

    Maurice

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 19, 2015 1:24 PM