ef core(code first) 通用性遷移步驟
阿新 • • 發佈:2019-01-31
- 建立Models類
- 建立DbContext派生類
- 新增Provider的nuget包,這裡以mysql為例:
- Pomelo.EntityFrameworkCore.MySql
- Pomelo.EntityFrameworkCore.MySql.Design
- 在dbcontext的OnConfiguring重寫方法中新增
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseMySql(ConnectString);
}
- 在其它地方配置也可以,比如在AspNetCore專案中,通常在Startup類中配置
dotnet ef migrations add {migration name} -p {dbcontext project} -s {host startup project}
dotnet ef database update -p {dbcontext project} -s {host startup project}