Pomelo.entityframeworkcore.mysql: Logging not working on .NET Core 3.1

Created on 14 Feb 2020  路  1Comment  路  Source: PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

Steps to reproduce

Here you can find a project to reproduce the issue: project.zip

The issue

Running any query on a dbcontext setup with Pomelo yields no logs (even when specifying low log levels) of the queries being run (as it used to in .NET Core 2).

Further technical details

MySQL version: 8.0
Operating system: macOS X 10.14, Ubuntu 18.04, Docker
Pomelo.EntityFrameworkCore.MySql version: 3.1.1
Microsoft.AspNetCore.App version: 3.1
Reference issue on main EF Core repo: https://github.com/dotnet/efcore/issues/18418#issuecomment-585672322

closed-question type-question

Most helpful comment

You need to provide a constructor with a DbContextOptions options parameter in your MainContext class and need to use the options as a base, if you want to add some by yourself.

Currently you build a completely new DbContextOptions object and thereby ignore everything that got setup before by EF Core and ASP.NET.

To fix your code:

``c# public class MainContext : DbContext { public MainContext( DbContextOptions options, // <-- addDbContextOptions options` parameter
IConfiguration config)
: base(GetDbContextOptions(options, config))
{
}

private static DbContextOptions GetDbContextOptions(
    DbContextOptions options, // <-- add `DbContextOptions options` parameter
    IConfiguration config)
{
    var connectionString = config.GetConnectionString("DefaultConnection");
    var dbContextOptionsBuilder =
        new DbContextOptionsBuilder(options); // <-- create builder from `options`
    dbContextOptionsBuilder.UseMySql(connectionString,
        mySqlOptions =>
        {
            mySqlOptions.ServerVersion(new Version(8, 0), ServerType.MySql);
        });
    return dbContextOptionsBuilder.Options;
}

// ...

}


Even better would be, if you would use `OnConfiguring(DbContextOptionsBuilder optionsBuilder)` instead of directly configuring your options in the constructor.

Your class would simplify like this:

```c#
public class MainContext : DbContext
{
    private readonly IConfiguration _config;

    public MainContext(
        DbContextOptions options,
        IConfiguration config)
        : base(options)
    {
        _config = config;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = _config.GetConnectionString("DefaultConnection");

        optionsBuilder.UseMySql(connectionString,
            mySqlOptions => mySqlOptions.ServerVersion(new Version(8, 0), ServerType.MySql));
    }

    // ...
}

>All comments

You need to provide a constructor with a DbContextOptions options parameter in your MainContext class and need to use the options as a base, if you want to add some by yourself.

Currently you build a completely new DbContextOptions object and thereby ignore everything that got setup before by EF Core and ASP.NET.

To fix your code:

``c# public class MainContext : DbContext { public MainContext( DbContextOptions options, // <-- addDbContextOptions options` parameter
IConfiguration config)
: base(GetDbContextOptions(options, config))
{
}

private static DbContextOptions GetDbContextOptions(
    DbContextOptions options, // <-- add `DbContextOptions options` parameter
    IConfiguration config)
{
    var connectionString = config.GetConnectionString("DefaultConnection");
    var dbContextOptionsBuilder =
        new DbContextOptionsBuilder(options); // <-- create builder from `options`
    dbContextOptionsBuilder.UseMySql(connectionString,
        mySqlOptions =>
        {
            mySqlOptions.ServerVersion(new Version(8, 0), ServerType.MySql);
        });
    return dbContextOptionsBuilder.Options;
}

// ...

}


Even better would be, if you would use `OnConfiguring(DbContextOptionsBuilder optionsBuilder)` instead of directly configuring your options in the constructor.

Your class would simplify like this:

```c#
public class MainContext : DbContext
{
    private readonly IConfiguration _config;

    public MainContext(
        DbContextOptions options,
        IConfiguration config)
        : base(options)
    {
        _config = config;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = _config.GetConnectionString("DefaultConnection");

        optionsBuilder.UseMySql(connectionString,
            mySqlOptions => mySqlOptions.ServerVersion(new Version(8, 0), ServerType.MySql));
    }

    // ...
}
Was this page helpful?
0 / 5 - 0 ratings