Exception when connect to MySQL using Pomelo Entity Framework Core

496 views Asked by At

I've a class library project and it connects to MySQL using Pomelo Entity Framework core. But it gets this exception An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call. when connect to Mysql database.

The issue can't not to reproduce because it only occurs in my client side and but not in my dev environment. The only difference between 2 environments is that I use Windows 10 and my client uses Windows 11.

Step working:

  1. Create a class library for DAL has HQPCDbContext, MainUserConfiguration and entity file
  2. Create another class library has Program.cs file (main project)
  3. Reference the DAL project in main project

This is DAL.csproj file:

   <Project Sdk="Microsoft.NET.Sdk">
       <PropertyGroup>
            <TargetFramework>net5.0</TargetFramework>
       </PropertyGroup>

       <ItemGroup>
            <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.17" />
            <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.4" />
       </ItemGroup>

   </Project>

This is Program.cs file:

string dbConnection = "server=localhost;port=3306;Uid=root;Pwd=mysql;database=test;"
var optionsBuilder = new DbContextOptionsBuilder<HQPCDbContext>();
optionsBuilder.UseMySql(dbConnection, MySqlServerVersion.LatestSupportedServerVersion);
var _context = new HQPCDbContext(optionsBuilder.Options);
var user = _context.MainUsers.FirstOrDefault(x => x.UserName == "abc");

This is HQPCDbContext file:

public class HQPCDbContext : DbContext
{
    public HQPCDbContext(DbContextOptions options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new MainUserConfiguration());       
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<MainUser> MainUsers { get; set; }
}

And this is MainUserConfiguration file:

public class MainUserConfiguration : IEntityTypeConfiguration<MainUser>
{
    public void Configure(EntityTypeBuilder<MainUser> builder)
    {
        builder.ToTable("MainUser");
        builder.HasKey(x => x.Id);            
    }
}

What is this issue and how to fix it? Everyone please help me!

I'm using .Net5, Pomelo.EntityFrameworkCore.MySql ver 5.0.4, Mysql ver 8.0.30, Windows 11

0

There are 0 answers