使用EF Core架构MySql数据库时出现问题 - 方法未找到:Void Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping

12
我正在尝试使用MySql.Data.EntityFrameworkCoreMicrosoft.EntityFrameworkCore在Visual Studio 2019的.NET Core 3.1上首先生成一个MySql数据库代码。然而,我一直收到以下错误信息:

Method not found: 'Void Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping..ctor(System.String, System.Type, System.Nullable1<System.Data.DbType>, Boolean, System.Nullable1)'。

我查看了GitHub上EF Core的源代码,但找不到与该签名匹配的方法 - 我是否漏掉了什么?
我的脚手架代码是:

dotnet ef dbcontext scaffold "Server=localhost;Database=database;Uid=myuid;" MySql.Data.EntityFrameworkCore -o Solution --project Solution.Project.Namespace

我也尝试过

dotnet ef dbcontext scaffold "Server=localhost;Database=database;Uid=myuid;" MySQL.Data.EntityFrameworkCore -o Solution --project Solution.Project.Namespace

我尝试通过开发人员 PowerShell 和包管理器控制台运行这两个命令。 结果相同。

我的 DbContext 是

using Solution.Data.Models;
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore; // I tried using MySql.Data.EntityFrameworkCore as well
using System;
using System.Collections.Generic;
using System.Text;

namespace Solution.Backend
{
    public class SolutionDBContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Journal> Journals { get; set; }
        public DbSet<JournalComment> JournalComments { get; set; }
        public DbSet<CartItem> CartItems { get; set; }
        public DbSet<Order> Orders { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySQL("Server=localhost;Database=database;Uid=myuid;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}

我的DbContext代码在一个名为Solution.Backend的项目中,我的模型在一个名为Solution.Data的项目中。

如果我注释掉所有的DbSet并尝试仅运行包含OnConfiguringOnModelCreating代码的DbContext,我会得到相同的结果。我是否遗漏了一些明显的东西,还是有什么地方我没有注意到,或者这是一个已知的错误?我在网上没有找到关于这个具体问题的任何信息。在此提前感谢您的帮助!


1
确保您正在使用正确的命名空间。 命名空间相当于文件夹名称,类相当于文件名。 当您拥有唯一的类名时,VS将自动找到它并编译。 当您有重复的类名时,您必须指定完整的命名空间和类名。 - jdweng
你能更具体一些吗 - 哪个命名空间有问题?在 dotnet ef dbcontext scaffold "Server=localhost;Database=database;Uid=myuid;" MySql.Data.EntityFrameworkCore -o *Solution* --project *Solution.Project.Namespace* 中,我尝试将其更改为 -o Solution.Project.Namespace,但它仍然出现了相同的问题。 - Schalk van der Merwe
代码更改。它将类似于以下内容:Solution.Data.Solution.Backend.SolutionDBContext.Users。 - jdweng
1个回答

26

MySql.Data.EntityFrameworkCore 8.0.22 只与 Microsoft.EntityFrameworkCore 3.1 兼容。降级所有 Microsoft.EntityFramework 包从 5.0.0 到 3.1.10 以解决错误。

随着 MySQL Connector/NET 8.0.23 的发布于 2021 年 1 月 18 日,Oracle 现在发布了一个不同的 NuGet 包,与 Microsoft.EntityFramework 5.0 兼容: MySql.EntityFrameworkCore

查看版本兼容性的表格

或者,您可以尝试切换到 Pomelo.EntityFrameworkCore.MySql 5.0.0-alpha.2(或更高版本);请参阅其兼容软件包版本的表格


2
我为这个问题苦恼了两天。你救了我的命。 - s.k.paul
3
这个就行了:安装包Microsoft.EntityFrameworkCore.Tools -version 3.1.10即可。 - Rob Sedgwick
除了降级实体框架,还有其他的方式吗?因为我真的不能放弃5.0.0版本。 - KingKong BigBong
1
@KingKongBigBong 是的,请改用 MySql.EntityFrameworkCore 包。 - Bradley Grainger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接