Error accessing data in Mysql with EF6

Asked

Viewed 1,126 times

0

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

Falha no método >'MySql.Data.Entity.EFMySqlCommand.set_DbConnection(System.Data.Common.DbConnection)' ao tentar acessar o método 'MySql.Data.MySqlClient.MySqlConnection.get_Settings()'.

I tried searching the internet but I couldn’t 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, connector version . net 8.0.10-rc and plugin version of visual studio 2.0.5 M4

  • how you set up the connection?

  • I set it up right by ado.net

  • So show the whole sequence, the problem seems to me to be at this point ... only the error itself has no way to be sure of anything!

  • All right, buddy, I posted the whole class.

  • this -> remoteEntities

  • My entity, you’re wrong?

  • can be so much, maybe the driver, maybe the connection I don’t know ... maybe ... it’s hard to reproduce your mistake.

  • What is strange is that I remove the connector, the plugin, the connection to the bank, remove everything and still when I add it again from the error.

Show 3 more comments

1 answer

2


I solved the problem by following the steps of another question I had posted here on Sopt. Link: Mysql net Connector and Entity Framework problem

When creating the entity model, App.config creates the following dependency:

<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 really installed the RC version is returned this error. Changing the dependency error disappears.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.