将实体框架与Pomelo一起使用时MySqlParameter类型冲突 [英] MySqlParameter type conflict when using Entity Framework with Pomelo

查看:136
本文介绍了将实体框架与Pomelo一起使用时MySqlParameter类型冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DAL.EntityFramework项目,该项目安装了Pomelo.EntityFrameworkCore.MySql包.我也有一个DAL.MySQL程序包,其中安装了MySql.Data程序包.第二个项目是针对与EntityFramework不相关的一般MySQL东西.

I have a DAL.EntityFramework project which has the Pomelo.EntityFrameworkCore.MySql package installed. I also have a DAL.MySQL package which has the MySql.Data package installed. This second project is for general MySQL stuff which is not related to EntityFramework.

在DAL.EntityFramework中,我有一种利用ADO.NET的方法,以便可以对数据库执行INSERT ... ON DUPLICATE KEY UPDATE操作(否则无法使用).

In DAL.EntityFramework I have a method which is utilising ADO.NET so that I can perform a INSERT ... ON DUPLICATE KEY UPDATE operation against my database (which isn't available otherwise).

public async Task<int> SmartUpsert(UserDetails user)
{
    var dbQuery = this.queryProvider.SmartUpsert(user);

    using (var command = this.Context.Database.GetDbConnection().CreateCommand())
    {
        command.CommandText = dbQuery.Query;
        command.CommandType = System.Data.CommandType.Text;
        command.Parameters.AddRange(dbQuery.Params.ToArray());

        this.Context.Database.OpenConnection();
        command.ExecuteNonQuery();
        this.Context.Database.CloseConnection();

        return Convert.ToInt32(command.Parameters["@Output"].Value.ToString());
    }
}

queryProvider被注入到类中,实现在TAP.MySQL中.这用于提供所需的特定于提供程序的SQL,而无需将EF项目键入特定的SQL提供程序.它还会创建参数集合(因为它们也是特定于提供程序的).

queryProvider is injected into the class and the implementation is in TAP.MySQL. This is used to provide the provider-specific SQL that's needed without typing the EF project to a particular SQL provider. It also creates the parameter collection (as these are provider-specific too).

public DbQuery SmartUpsert(UserDetails user)
{
    var query = new DbQuery
    {
        Query = "SmartUpsertUserDetails"
    };

    var sqlParams = new List<MySqlParameter>()
    {
        new MySqlParameter("@id", user.UserId),
        new MySqlParameter("@title", user.Title),
        new MySqlParameter("@name", user.Name),
        new MySqlParameter("@surname", user.Surname),
        new MySqlParameter("@email", user.Email)
    };

    var outputParam = new MySqlParameter();
    outputParam.ParameterName = "@result";
    outputParam.MySqlDbType = MySqlDbType.Int32;
    outputParam.Direction = ParameterDirection.Output;
    sqlParams.Add(outputParam);

    query.Params = sqlParams;

    return query;
}

运行此代码时,它在command.Parameters.AddRange(dbQuery.Params.ToArray());行上失败,但有以下异常:

When running this code, it fails on the command.Parameters.AddRange(dbQuery.Params.ToArray()); line with the following exception:

[A] MySql.Data.MySqlClient.MySqlParameter无法强制转换为 [B] MySql.Data.MySqlClient.MySqlParameter.类型A起源于 'MySql.Data,版本= 8.0.13.0,文化=中性, 位置默认"上下文中的PublicKeyToken = c5687fc88969c44d" 'C:\ Users \ Andy.nuget \ packages \ mysql.data \ 8.0.13 \ lib \ netcoreapp2.0 \ MySql.Data.dll'. 类型B源自"MySqlConnector,版本= 0.47.1.0, 在上下文中,文化=中性,PublicKeyToken = d33d3e53aa5f8c92' 位置为默认" 'C:\ Users \ Andy.nuget \ packages \ mysqlconnector \ 0.47.1 \ lib \ netcoreapp2.1 \ MySqlConnector.dll'.

[A]MySql.Data.MySqlClient.MySqlParameter cannot be cast to [B]MySql.Data.MySqlClient.MySqlParameter. Type A originates from 'MySql.Data, Version=8.0.13.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' in the context 'Default' at location 'C:\Users\Andy.nuget\packages\mysql.data\8.0.13\lib\netcoreapp2.0\MySql.Data.dll'. Type B originates from 'MySqlConnector, Version=0.47.1.0, Culture=neutral, PublicKeyToken=d33d3e53aa5f8c92' in the context 'Default' at location 'C:\Users\Andy.nuget\packages\mysqlconnector\0.47.1\lib\netcoreapp2.1\MySqlConnector.dll'.

Pomelo.EntityFrameworkCore.MySql附带的MySqlConnector软件包中的MySqlParameter和DAL.MySQL项目中安装的MySql.Data中的MySqlParameter之间似乎混淆了.

It seems that it's getting confused between the MySqlParameter in the MySqlConnector package that comes with Pomelo.EntityFrameworkCore.MySql and the MySqlParameter in MySql.Data installed on my DAL.MySQL project.

在这种情况下我该怎么办?我不完全确定为什么Pomelo使用现有MySql类的重复项,但不管怎样.

What am I to do in this scenario? I'm not entirely sure why Pomelo is using duplicates of existing MySql classes but whatever.

推荐答案

柚子使用的是 MySqlConnector 而不是Oracle自己的软件包.在 MySqlConnector项目站点上概述了这种好处:

Pomelo is using MySqlConnector instead of Oracle’s own package. The benefits of this are outlined on the MySqlConnector project site:

为什么通过Oracle的Connector/NET使用MySqlConnector?

MySqlConnector是 MySQL协议的无尘室重新实现. ,它不是基于 Oracle的Connector/NET .

  • MySqlConnector:完全异步I/O
  • Oracle的Connector/NET :异步调用映射到同步I/O
  • MySqlConnector: Fully asynchronous I/O
  • Oracle’s Connector/NET: Async calls map to synchronous I/O
  • MySqlConnector: GitHub
  • Oracle的Connector/NET :封闭的开发路线图.可以在 GitHub 上查看代码,某些问题在
  • MySqlConnector: Open and Collaborative Development on GitHub
  • Oracle’s Connector/NET: Closed Development Roadmap. Code is viewable on GitHub, some issues addressed in forums
  • MySqlConnector: The MIT License
  • Oracle’s Connector/NET: GPLv2 with FOSS Exception; or commercial license

因此,基本上,MySqlConnector只是一个更好的选择.我对Oracle自己的软件包的经验还在于,它们的开发速度很慢,而且您通常不知道它们在做什么.例如,当EF Core 2.0发布时,Oracle的EF提供程序正在缓慢更新,而当他们发布它时,它不能正常工作.但是GitHub上的源代码尚未更新,因此您甚至无法确定那里发生了什么. Pomelo和MySqlConnector更加开放,并且通常更易于使用.

So basically, MySqlConnector is just a much better choice. My experience with Oracle’s own packages is also that they are slow to evolve and you have generally no clue what they are working on. For example, when EF Core 2.0 was released, Oracle’s EF provider was slowly updating and when they did release it, it wasn’t working properly. But the source code on GitHub wasn’t updated yet, so you couldn’t even tell what was going on there. Pomelo and MySqlConnector are much more open and generally nicer to use.

根据此问题,在MySqlConnector中重用相同的名称空间是故意的选择充当Oracle连接器的替代产品.虽然同时使用两者不是故意的用例,但是如果您真的别无选择,可以这样做.

As per this issue reusing the same namespaces in MySqlConnector was a deliberate choice to act as a drop-in replacement for Oracle’s connector. Using both at the same time is not an intentional use case, although it is possible to do so if you really have no other choice.

对于您的项目,这意味着您还应该考虑迁移到MySqlConnector.这样,您可以轻松解决冲突.

For your project, this means that you should consider also moving to MySqlConnector. That way, you can resolve the conflicts easily.

如MySqlConnector的作者 Bradley Grainger 所述:

As commented by Bradley Grainger, the author of MySqlConnector:

MySql.Data API的一些部分尚未实现(MySqlScript可能是最大的部分,但很少使用);大多数人发现它与MySql.Data的使用100%兼容.在此处检查迁移文档: https://mysqlconnector.net/tutorials/migrating-from -connector-net/.如果您的代码依赖于各种MySql.Data行为,则可能需要更改一些连接字符串设置.

There are a few parts of the MySql.Data API that aren't implemented (MySqlScript may be the biggest one, but it's very rarely used); most people find that it's 100% compatible with their use of MySql.Data. Check the migration docs here: https://mysqlconnector.net/tutorials/migrating-from-connector-net/. You may need to change some connection string settings if your code relied on various MySql.Data behaviours.

这篇关于将实体框架与Pomelo一起使用时MySqlParameter类型冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆