asp 网络核心 mysql 与 entity framework 的连接

asp net core mysql connection with entity framework

我想将 entity framework 添加到我的 API 项目中,我的数据库是 mysql

我该怎么做

.net 核心 version:2.1.2

您需要使用MySql.Data.EntityFrameworkCore

1) Install the MySql.Data.EntityFrameworkCore NuGet package.

2) In the class that derives from the DbContext class, override the OnConfiguring method to set the MySQL data provider with UseMySQL. The following example shows how to set the provider using a generic connection string in C#.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{   
    optionsBuilder.UseMySQL("server=localhost;database=library;user=user;password=password");
}

查看可用的 MySQL documentation 了解更多详情。