.net Core CodeFirst结合MySql

1.下载依赖包

Microsoft.EntityFrameworkCore.Design
​
Microsoft.EntityFrameworkCore.Tools
​
MySql.EntityFrameworkCore

2.配置appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "stu": "server=localhost;user id=root;database=Student;Pwd=123456;port=3306"
  }
}
​

3.创建数据上下文

        public class StuDbContext:DbContext
    {
        public StuDbContext(DbContextOptions<StuDbContext>options):base(options)
        {
            
        }
​
        //映射
        public DbSet<Student> Student { get; set; }
    }

4.Startup

services.AddDbContext<StuDbContext>(option =>    option.UseMySQL(Configuration.GetConnectionString("stu")));
services.AddControllersWithViews();

5.迁移

1.Add-Migration ×××××

2.Update-Database

上一篇:个人记录:对于python学习的反思和总结(一)


下一篇:无法连接到Redis:java.net.ConnectException: Connection refused: no further information