How to connect MySQL use EntityFrameworkCore (Pomelo.EntityFrameworkCore.MySql) in ASP.NET Core

Install packages Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Design(for dotnet CLI), Microsoft.EntityFrameworkCore.Tools(for package manage console), Pomelo.EntityFrameworkCore.MySql

 

Data folder > ApplicationDbContext.cs

public class ApplicationDbContext : DbContext
{

    // constructor(must)
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }

    // table
    public DbSet<ToDo> ToDos { get; set; }
}

 

Program.cs

string connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
创建时间:6/26/2022 12:47:18 AM 修改时间:6/26/2022 12:53:48 AM