1.創建項目
創建Data文件夾
創建實體類Students/cs
public class Students { public Guid Id { get; set; } public string Name { get; set; } public int Age { get; set; } }
創建MyDbContext
public class MyDbContext:DbContext { public MyDbContext(DbContextOptions<MyDbContext> options):base(options) { } public DbSet<Students> Students { get; set; } }
在appsetting.json配置連接字符串
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "ConnectionStrings": { "DefaultConnection": "server=.;database=Students;uid=sa;pwd=123456;" } }
使用 code first 創建數據庫
輸入命名
Add-Migration init