简体   繁体   English

如何在通过 PMC for mysql 添加 ef 核心迁移时修复错误?

[英]How to fix error when adding an ef core migration through the PMC for mysql?

I want to switch from using SQL Server to MySql.我想从使用 SQL Server 切换到 MySql。 I have already created migrations for SQL Server and have applied them to the DB.我已经为 SQL Server 创建了迁移并将它们应用于数据库。 I have added the Pomelo Mysql package and now want to create migrations for a MySql db.我已经添加了 Pomelo Mysql 包,现在想要为 MySql 数据库创建迁移。 My problem is that I am receiving an error in the Package Manager Console when trying to use the add-migration command.我的问题是我在尝试使用 add-migration 命令时在包管理器控制台中收到错误。

  • It's a.Net Core 2.2 project这是一个 .Net Core 2.2 项目
  • Pomelo.EntityFrameworkCore.MySql 2.2.0 has been added to the project Pomelo.EntityFrameworkCore.MySql 2.2.0 已添加到项目中

Startup / ConfigureServices启动/配置服务

services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), 
                mySqlOptions => { mySqlOptions.ServerVersion(new Version(8, 0, 16), ServerType.MySql); }));

csproj项目

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" PrivateAssets="All" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
  </ItemGroup>

ApplicationDbContext Class ApplicationDbContext 类

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseMySql("Server=localhost;Database=myDbUid=myUserId;");
}

I am receiving the following error when trying to add a migration.尝试添加迁移时收到以下错误。 "An item with the same key has already been added. Key: Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal.MySqlOptionsExtension" Does this error have anything to do with the fact I have previous migrations for MS SQL Server? “已添加具有相同密钥的项目。密钥:Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal.MySqlOptionsExtension” 此错误与我之前为 MS SQL Server 进行过迁移有关吗? Any help would be much appreciated.任何帮助将非常感激。

I was having the same problem.我遇到了同样的问题。 For me the answer was to remove the OnConfiguring() method from my Context class.对我来说,答案是从我的 Context 类中删除OnConfiguring()方法。

I had two projects - one containing the DB code and the migrations, the other being an ASP.NET Core web project to view the data.我有两个项目 - 一个包含数据库代码和迁移,另一个是用于查看数据的 ASP.NET Core Web 项目。

I had the following in Startup.cs in my web project:我的 Web 项目的 Startup.cs 中有以下内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddDbContext<EFCoreExampleContext>(o => o.UseMySql("server=etc..."));
}

In my DB project I had this in the DbContext-derived class:在我的数据库项目中,我在 DbContext 派生类中有这个:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseMySql("server=etc...");
}

Even though I was using Update-Database -Project MyDbProj , it was using the connection string in the other project.即使我使用的是Update-Database -Project MyDbProj ,它也使用了另一个项目中的连接字符串。 If I removed the AddDbContext line from Startup.cs, I just got a different error.如果我从 Startup.cs 中删除AddDbContext行,我只会收到一个不同的错误。 Removing the OnConfiguring method was the only way to get it to work.删除OnConfiguring方法是让它工作的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM