Search code examples
c#asp.net-coremysql-connectorpomelo-entityframeworkcore-mysql

MySqlServerVersion Error in .NET Core 3.1 in Startup.cs


I'm facing error: CS0246 in line new MySqlServerVersion(new Version(8, 0, 40))

The following is my Startup.cs:

using Enterprise.EmployeeManagement.DAL.Data;
using Enterprise.EmployeeManagement.Core.Services;
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Microsoft.Extensions.Hosting;
using Pomelo.EntityFrameworkCore.MySql.Storage;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // Configure Entity Framework to use MySQL with Pomelo and MySqlConnector
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseMySql(
                Configuration.GetConnectionString("DefaultConnection"),
                new MySqlServerVersion(new Version(8, 0, 40))  
            )
        );

        // Register the UserService as scoped
        services.AddScoped<UserService>();

        // Add controllers with views
        services.AddControllersWithViews();
    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
           https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

The following packages seems to be disabled/grey in the editor:

using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Pomelo.EntityFrameworkCore.MySql.Storage;
  • I'm using .NET Core 3.1 (strictly the same version), Pomelo.EntityFrameworkCore.MySql 3.2.7
  • Using Three Tier Architecture with Core, DAL and Web Layers (Startup.cs exists in WEb Layer)

I tried doing following things:

  • Tried reinstalling the packages with the required version and made sure they are in Dependency/Packages in Web Layer
  • Tried dotnet clean , dotnet build , dotnet resotre serveral times.

Solution

  • The newer MySqlServerVersion class, introduced in later Pomelo versions(e.g 5.0.0 can work fine). In older versions(3.2.7), the MySqlServerVersion may not work as directly. You can specify the server version as a string like below:

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
    mySqlOptions => mySqlOptions.ServerVersion("8.0.40-mysql"))            
        );