简体   繁体   中英

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. I have already created migrations for SQL Server and have applied them to the DB. I have added the Pomelo Mysql package and now want to create migrations for a MySql db. My problem is that I am receiving an error in the Package Manager Console when trying to use the add-migration command.

  • It's a.Net Core 2.2 project
  • Pomelo.EntityFrameworkCore.MySql 2.2.0 has been added to the project

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

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? 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.

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.

I had the following in Startup.cs in my web project:

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:

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. If I removed the AddDbContext line from Startup.cs, I just got a different error. Removing the OnConfiguring method was the only way to get it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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