【发布时间】:2021-03-19 02:07:18
【问题描述】:
我正在尝试首先在 Visual Studio 2019 的 .NET Core 3.1 上使用 MySql.Data.EntityFrameworkCore 和 Microsoft.EntityFrameworkCore 构建 MySql 数据库代码。但是,我不断收到以下错误:
找不到方法:'无效 Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping..ctor(System.String, System.Type, System.Nullable
1<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
我尝试通过 Developer PowerShell 和 Package Manager Console 运行这两个命令。结果一样。
我的 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
的项目中如果我注释掉所有 DbSets 并尝试在 DbContext 中除了 OnConfiguring 和 OnModelCreating 代码之外没有任何东西运行它,我会得到相同的结果。我是否遗漏了一些明显的东西,或者我遗漏了什么,或者这是一个已知的错误?我在任何地方都没有在网上找到有关此特定问题的任何信息。我很感激这里的一些帮助!提前致谢。
【问题讨论】:
-
确保您使用的是正确的命名空间。命名空间等同于文件夹名称,类等同于文件名。 VS 当你有一个唯一的类名时,编译器会自动找到它。当您有重复的类名时,您必须指定完整的命名空间和类名。
-
您能否更具体一点 - 哪个命名空间是问题?在
dotnet ef dbcontext scaffold "Server=localhost;Database=database;Uid=myuid;" MySql.Data.EntityFrameworkCore -o *Solution* --project *Solution.Project.Namespace*我尝试更改为 -o Solution.*Project*.*Namespace* 但它给了我同样的问题。 -
代码更改。我看起来类似于以下内容:Solution.Data.Solution.Backend.SolutionDBContext.Users
标签: c# mysql entity-framework-core .net-core-3.1 ef-core-3.1