【问题标题】:MySql EntityFrameworkCore System.TypeLoadExceptionMySql EntityFrameworkCore System.TypeLoadException
【发布时间】:2018-01-08 10:23:15
【问题描述】:

我正在尝试使用以下代码将我的 Web API 连接到 MySql 数据库:

public class Startup
{       
    public void ConfigureServices(IServiceCollection services)
    {
        string conn_string = "server=server;database=database;uid=uid;pwd=pwd;";
        MySqlConnection conn = new MySqlConnection(conn_string);
        services.AddDbContext<TaskContext>(options =>
        {
            options.UseMySQL(conn);
        });
        services.AddMvc();
    }      
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }
}

但我总是收到带有此描述的 System.TypeLoadException:

System.TypeLoadException:类型中的“方法“克隆” 'MySQL.Data.EntityFrameworkCore.Infraestructure.Internal.MySQLOptionsExtension' 从程序集'MySql.Data.EntityFrameworkCore,版本= 8.0.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' 没有 实施。'

我正在使用 Microsoft Visual Studio Community 2017 Preview (2),并且我的项目在 .NET Core 2.0 中。我还使用 MySql.Data.EntityFrameworkCore.dll 和 Microsoft.AspNetCore.All (2.0.0-preview2-final)。我已经为 MySql 更改了很多次库,但没有成功。

知道为什么这总是发生吗?可能是什么原因?

【问题讨论】:

    标签: mysql entity-framework-core asp.net-core-webapi asp.net-core-2.0


    【解决方案1】:

    好吧,事实证明您不能将 MySql.Data.EntityFrameworkCore 用于 .Net Core 2.0(目前)。我必须回到 .Net Core 1.3 才能让它工作......我们也许可以在不久的将来使用它!

    【讨论】:

    • 要针对 .NET Core 2.0,您必须使用同样针对 .NETStandard 2.0 的提供程序,例如 nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/…
    • 我试过 Pomelo.EntityFrameworkCore.MySql 2.0.0-preview2-final 并且有同样的错误,但我没有尝试 Pomelo.EntityFrameworkCore.MySql 2.0.0-preview3-10054。感谢您的帮助。
    • 很高兴它为您节省了一些时间!
    【解决方案2】:

    使用 Pomelo.EntityFrameworkCore.MySql 2.0.0。 使用 .NET Core 2.0 对我来说效果很好

    【讨论】:

      【解决方案3】:

      添加“Pomelo.EntityFrameworkCore.MySql”包。 在 Stetup.cs & appsettings.json & DbContext 中:

      services.AddDbContext<mvccoreContext>(options =>
               options.UseMySql(Configuration.GetConnectionString("DefaultConnection")
              ));
      
      
           {
            "ConnectionStrings": {
              "DefaultConnection": "Server=localhost;Database=mvccore;User=root;Password=;"
            },
            "Logging": {
              "LogLevel": {
                "Default": "Warning"
              }
            },
            "AllowedHosts": "*"
          }
      
      
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              if (!optionsBuilder.IsConfigured)
              {
                  optionsBuilder.UseMySql("");
              }
          }
      

      【讨论】:

        【解决方案4】:

        MySql.Data.EntityFrameworkCore 支持(并且仅兼容)EF Core 2.1。使用不匹配的 EF Core 和 DB 提供程序库将导致类似于您报告的错误。

        如果您需要与 EF Core 3.0 兼容的 MySQL EF Core 提供程序,您现在唯一的选择是 https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql 的 3.0.0-rc1 版本(请参阅 https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#compatibility 的兼容性表)! )。

        否则,如果您想坚持使用 MySql.Data.EntityFrameworkCore,则需要回滚到 EF Core 2.1。

        【讨论】:

          猜你喜欢
          • 2017-05-30
          • 2018-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-23
          • 2017-09-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多