数据库
首页 > 数据库> > .net Core CodeFirst结合MySql

.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

标签:Core,CodeFirst,Information,Microsoft,EntityFrameworkCore,Student,StuDbContext,ne
来源: https://www.cnblogs.com/y614/p/15321828.html