no overload for method 'usemysql' takes 1 arguments

1k Views Asked by At
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {optionsBuilder.UseMySql(connectionString: @"server=localhost;userid=root;password=***;database=coursesystemdb");
    }

This is the dbcontext class that ı had used optionsbuilder.usemysql and after upgrade my core to 5.0 and ı take error no overload for method usemsql takes 1 arguments. How can ı solve this?

here is also startup.class

public void ConfigureServices(IServiceCollection services)
    {services.AddCors(options =>
        {options.AddPolicy("AllowOrigin", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

        });

Thanks a lot

1

There are 1 best solutions below

1
On

Take a look at the sample code on our front page/readme:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Replace with your connection string.
        var connectionString = "server=localhost;user=root;password=1234;database=ef";

        // Replace with your server version and type.
        // Use 'MariaDbServerVersion' for MariaDB.
        // Alternatively, use 'ServerVersion.AutoDetect(connectionString)'.
        // For common usages, see pull request #1233.
        var serverVersion = new MySqlServerVersion(new Version(8, 0, 21));

        // Replace 'YourDbContext' with the name of your own DbContext derived class.
        services.AddDbContext<YourDbContext>(
            dbContextOptions => dbContextOptions
                .UseMySql(connectionString, serverVersion)
                .EnableSensitiveDataLogging() // <-- These two calls are optional but help
                .EnableDetailedErrors()       // <-- with debugging (remove for production).
        );
    }
}

It shows you how to call UseMySql() and that it expects 2 parameters: the connection string and the version of the database server that you are using.

You can also let the server version be detected automatically, instead of specifying it explicitly (showed using OnConfiguring() here, which you used in your OP):

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    var connectionString = @"server=localhost;userid=root;password=***;database=coursesystemdb";
    var serverVersion = ServerVersion.AutoDetect(connectionString);

    optionsBuilder
        .UseMySql(connectionString, serverVersion)
        .EnableSensitiveDataLogging() // <-- These two calls are optional but help
        .EnableDetailedErrors();      // <-- with debugging (remove for production).
}

We added a mandatory serverVersion parameter to the UseMySql() method in 5.0, because Pomelo supports MySQL and MariaDB in different versions and needs to know which one you use, so that it can support your database server in the best way possible.