在mac上使用Pomelo.EntityFrameworkCore.MySql连接mysql



我试图使用柚。entityframeworkcore . MySQL能够连接到我的MySQL数据库。我使用的是。net 6,它从项目中删除了startup.cs,并将所有内容合并到Program.cs.

下面的代码是我试图连接到我的数据库的方式,但它给了我一个错误与serverVersion。

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

var serverVersion = new MySqlServerVersion(new Version(8, 0, 28));
builder.Services.AddDbContext<BirdProjectContext>(
dbContextOptions => dbContextOptions
.UseMySQL(connectionString, serverVersion)
// The following three options help with debugging, but should
// be changed or removed for production.
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
);
我得到的错误是:

CS1503:参数3:cannot convert from 'Microsoft.EntityFrameworkCore. 'MySqlServerVersion'到'System.Action'

如果我试图从参数中删除服务器版本,我得到另一个错误:

CS0121:调用在以下方法或属性之间是不明确的:'Microsoft.EntityFrameworkCore. mysqldbcontextoptionsextension . usemysql (Microsoft.EntityFrameworkCore. mysqldbcontextoptionsextension . 'MySql.Data.EntityFrameworkCore.Infrastructure.MySQLDbContextOptionsBuilder>)'和'Microsoft.EntityFrameworkCore. mysqldbcontextoptionsextension . usemysql (Microsoft.EntityFrameworkCore. mysqldbcontextoptionsextension .)'。DbContextOptionsBuilder, string, System.Action)'

这个项目是使用SQL,现在我正试图转换为MySQL。

这些是我已经安装到项目中的包:

Nuget包

EntityFramework有一种动态获取DB服务器版本的方法,试试这种方式:

builder.Services.AddDbContext<BirdProjectContext>(
dbContextOptions => dbContextOptions
.UseMySQL(connectionString,  ServerVersion.AutoDetect(connectionString))
// The following three options help with debugging, but should
// be changed or removed for production.
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
);

您正在使用UseMySQL()方法,属于Oracle的provider

对于柚提供程序,使用UseMySql()方法(不同的大小写).

示例如何调用它,可以在我们的GitHub存储库的主页上找到,它使用正确的大小写:
// 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, 29));
// Replace 'YourDbContext' with the name of your own DbContext derived class.
services.AddDbContext<YourDbContext>(
dbContextOptions => dbContextOptions
.UseMySql(connectionString, serverVersion)
// The following three options help with debugging, but should
// be changed or removed for production.
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
);