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:
In my DB project I had this in the DbContext-derived class:
Even though I was using
Update-Database -Project MyDbProj, it was using the connection string in the other project. If I removed theAddDbContextline from Startup.cs, I just got a different error. Removing theOnConfiguringmethod was the only way to get it to work.