1.打开nuget包管理器,安装Mysql.Data.EntityFrameworkCore

2.测试代码

using Microsoft.EntityFrameworkCore;

using System;

using static System.Console;

//Model For Test

class User

{

    public int ID { get; set; }

    public string Name { get; set; }

    public string PassWord { get; set; }

}

class APPDBContext:DbContext

{

    public APPDBContext() { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

    => optionsBuilder.UseMySQL("server=localhost;database=test;uid=root;pwd=wjjxxnjq;charset=utf8;pooling=true;");//配置连接字符串

    public DbSet<User> User { get; set; }

}

class P

{

    public static void Main(string[] args)

    {

        using (APPDBContext dx = new APPDBContext())

        {

User us = new User()

            {

                ID = 1001,

Name = "我系渣渣辉",

PassWord = "zzh521666",

            };

            dx.User.Add(us);

            dx.SaveChanges();

        }

    }

}

3.打开数据库管理器看效果

注:实际开发连接字符串通常写在JSON序列化文件中,使用Configuation读出,此处仅为测试

12-22 17:56