1
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

4

1 回答 1

2

看看我们首页/自述文件上的示例代码:

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).
        );
    }
}

它向您展示了如何调用UseMySql()并且它需要 2 个参数:连接字符串和您正在使用的数据库服务器的版本。

您还可以让服务器版本自动检测,而不是显式指定它(使用OnConfiguring()此处显示,您在 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).
}

我们在 5.0 中给该方法添加了一个强制serverVersion参数UseMySql(),因为 Pomelo 支持不同版本的 MySQL 和 MariaDB,需要知道你使用的是哪一个,这样它才能以最好的方式支持你的数据库服务器。

于 2021-05-28T15:15:02.310 回答