1. 程式人生 > >ef core(code first) 通用性遷移步驟

ef core(code first) 通用性遷移步驟

  1. 建立Models類
  2. 建立DbContext派生類
  3. 新增Provider的nuget包,這裡以mysql為例:
    1. Pomelo.EntityFrameworkCore.MySql
    2. Pomelo.EntityFrameworkCore.MySql.Design
  4. 在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);
    }

    1. 在其它地方配置也可以,比如在AspNetCore專案中,通常在Startup類中配置
  5. dotnet ef migrations add {migration name} -p {dbcontext project} -s {host startup project}
  6. dotnet ef database update -p {dbcontext project} -s {host startup project}