1.首先新建 Asp.Net Core WebApi 項目
2.添加一下引用 :
2.1 Pomelo.EntityFrameworkCore.MySql(我用的Mysql 根據自己情況引用就行)
2.2 Microsoft.EntityFrameworkCore
2.3 Microsoft.EntityFrameworkCore.Design
3.使項目支持dotnet ef 工具以使用Migrations
3.1 手動修改csproj文件(手動添加是因為在nuget添加Microsoft.EntityFrameworkCore.Tools.DotNet 時報錯,估計是vs的問題),添加一下配置
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
4.打開CMD命令 cd到項目目錄下(C:\Users\Administrator\source\repos\CodeFirst\CodeFirst),執行
dotnet build
Microsoft (R) Build Engine version 15.1.545.13942 Copyright (C) Microsoft Corporation. All rights reserved. Startup.cs(45,13): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo.csproj] EntityFrameworkCoreMigrationsDemo -> C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\bin\Debug\netcoreapp1.0\EntityFrameworkCoreMigrationsDemo.dll Build succeeded. Startup.cs(45,13): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo.csproj] 1 Warning(s) 0 Error(s) Time Elapsed 00:00:04.76
5. 第4步完成后繼續執行
dotnet ef
可以看見獨角獸就說明引用成功了。距離勝利更近一步了
6.創建實例 StudentDbContext和相關實體類
using CodeFirst.Model.Entity; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; namespace CodeFirst.Model.Db { public class StudentDbContext: DbContext { public StudentDbContext(DbContextOptions<StudentDbContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySql( @"Server=localhost;Port=3306;Database=Policy;UId=root;Pwd=mysql.com"); } public DbSet<Student> Student { get; set; } public DbSet<Teacher> Teacher { get; set; } } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text; namespace CodeFirst.Model.Entity { public class Student { [Key] public int Id { get; set; } public string Name { get; set; } public Teacher Teach { get; set; } } }
using System; using System.Collections.Generic; using System.Text; namespace CodeFirst.Model.Entity { public class Teacher { public int Id { get; set; } public int Age { get; set; } public List<Student> Stus { get; set; } } }
7. 在Startup將StudentDbContext 注冊為服務
8.使用Migrations 新建數據庫初始化類DbInitializer
using CodeFirst.Model.Db; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace CodeFirst.Model { public class DbInitializer { public void InitializeAsync(StudentDbContext context) { //var migrations = await context.Database.GetPendingMigrationsAsync();//獲取未應用的Migrations,不必要,MigrateAsync方法會自動處理 context.Database.MigrateAsync();//根據Migrations修改/創建數據庫 } } }
9.在Startup.cs的Configure方法中,添加參數StudentContext context
,netcore會使用DI將DbContext注入到Configure方法,並添加對DbInitializer
的調用。
10.使用dotnet ef
命令創建Migrations:dotnet ef migrations add text text隨便起名
C:\Users\Administrator\source\repos\CodeFirst\CodeFirst>dotnet ef migrations add Initial Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:02.32 Done. To undo this action, use 'ef migrations remove'
如果執行dotnet ef migrations add Initial報錯 :

解決辦法執行下面
定位到csproject
PM> dotnet ef migrations script --verbose -i --project "C:\Users\Administrator\source\repos\CodeFirst\CodeFirst"
完了繼續操作就行
生成數據庫表:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
修改Student實體類加一個Sex字段
public string Sex { get; set; }
執行 (注意執行命令之前在控制台‘默認項目’列表選中DbContext所在的類庫,不然爆錯)
Add-Migration ModifyStudent
再執行
PM> Update-Database
數據庫已經更新
Demo源碼: Github