Error accessing data in Mysql with EF6

0

When I try to access the data in MySQL via lambda ( EF ), Visual Studio returns the following error:

  

Method 'MySql.Data.Entity.EFMySqlCommand.set_DbConnection (Method.Data.Common.DbConnection) failed' when trying to access method 'MySql.Data.MySqlClient.MySqlConnection.get_Settings ()'.

I tried searching the internet but did not find anything that could help me with this error. Follow the Main Controller Code

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using MySql.Data.Entity;
using System.Media;
using View.Model;
using System.Security.Cryptography;

namespace View
{
public class MainController
{
    remoteEntities remote = new remoteEntities();

    public bool validaLogin(string usuario, string senha)
    {
        string novaSenha = getMD5Hash(senha);
        var resultado = remote.Motorista.Select(x => new { x.ID, x.Login, x.Senha, x.Nome }).Where(u => u.Login == usuario && u.Senha == novaSenha).ToList();
        if (resultado.Count > 0)
            return true;
        else
            return false;
    }

    public static string getMD5Hash(string input)
    {
        var md5Hash = MD5.Create();
        // Converter a String para array de bytes, que é como a biblioteca trabalha.
        var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

        // Cria-se um StringBuilder para recompôr a string.
        var sBuilder = new StringBuilder();

        // Loop para formatar cada byte como uma String em hexadecimal
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        return sBuilder.ToString();
    }

    #region Testa Conexão

    [System.Runtime.InteropServices.DllImport("wininet.dll")]
    private static extern bool InternetGetConnectedState(out int Description, int ReservedValue);

    public static bool checkConnection()
    {
        int desc;
        bool hasConnection = InternetGetConnectedState(out desc, 0);
        if (hasConnection)
            hasConnection = webClient("http://zerohoravirtual.com/");

        return hasConnection;
    }

    private static bool webClient(string _url)
    {
        System.Net.WebRequest webReq;
        System.Net.WebResponse resp;
        webReq = System.Net.WebRequest.Create(_url);

        try
        {
            resp = webReq.GetResponse();
            resp.Close();
            webReq = null;
            return true;
        }
        catch
        {
            webReq = null;
            return false;
        }
    }

    #endregion
}

}

Update 1

Connection created with ADO.Net, .net 8.0.10-rc connector version and visual studio plugin version 2.0.5 m4

    
asked by anonymous 05.02.2018 / 00:25

1 answer

1

I solved the problem by following the steps of another question I posted here in SOpt. Link: Mysql net connector problem and Entity Framework

When creating the entity model, the following dependency is created in the App.config:

<dependentAssembly>
    <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-8.0.10.0" newVersion="8.0.10.0" />
  </dependentAssembly>

Change the dependency to:

<dependentAssembly>
    <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.9.11.0" newVersion="6.9.11.0" />
  </dependentAssembly>

Because when installed Mysql for Visual Studio in version 2.0.5 m4, it creates the dependency with the RC version of Mysql Connector / Net and as it is not actually installed the RC version is returned this error. By changing the dependency the error disappears.

    
06.02.2018 / 17:36