索引:html
目錄索引bash
1、描述:ide
在使用 Entity Framework Core 時,使用 CodeFirst 模式,ui
在 VS 中的 PMC(nuget 包管理 控制檯) 控制檯界面使用以下命令:spa
1 Install-Package Microsoft.EntityFrameworkCore.Tools 2 3 Add-Migration Initial 4 5 Update-Database
2、問題:3d
遇到的PowerShell 版本問題,以下:rest
The Entity Framework Core Package Manager Console Tools don't support PowerShell version 2.0. Upgrade to PowerShell version 3.0 or higher, restart Visual Studio, and try again.code
3、解決方法:htm
1) 下載4.0版本blog
https://www.microsoft.com/zh-CN/download/details.aspx?id=40855
2) 安裝下載完成的包:
3) 打開VS PMC 窗口,從新運行命令:
4、自動生成的代碼:
上下文代碼
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Linq; 6 7 using System.Threading.Tasks; 8 9 using Microsoft.EntityFrameworkCore; 10 11 12 13 namespace MvcMovie.Models 14 15 { 16 17 public class MvcMovieContext : DbContext 18 19 { 20 21 public MvcMovieContext (DbContextOptions<MvcMovieContext> options) 22 23 : base(options) 24 25 { 26 27 } 28 29 30 31 public DbSet<MvcMovie.Models.Movie> Movie { get; set; } 32 33 } 34 35 }
命令生成代碼
1 using System; 2 3 using System.Collections.Generic; 4 5 using Microsoft.EntityFrameworkCore.Migrations; 6 7 using Microsoft.EntityFrameworkCore.Metadata; 8 9 10 11 namespace MvcMovie.Migrations 12 13 { 14 15 public partial class Initial : Migration 16 17 { 18 19 protected override void Up(MigrationBuilder migrationBuilder) 20 21 { 22 23 migrationBuilder.CreateTable( 24 25 name: "Movie", 26 27 columns: table => new 28 29 { 30 31 ID = table.Column<int>(nullable: false) 32 33 .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), 34 35 Genre = table.Column<string>(nullable: true), 36 37 Price = table.Column<decimal>(nullable: false), 38 39 ReleaseDate = table.Column<DateTime>(nullable: false), 40 41 Title = table.Column<string>(nullable: true) 42 43 }, 44 45 constraints: table => 46 47 { 48 49 table.PrimaryKey("PK_Movie", x => x.ID); 50 51 }); 52 53 } 54 55 56 57 protected override void Down(MigrationBuilder migrationBuilder) 58 59 { 60 61 migrationBuilder.DropTable( 62 63 name: "Movie"); 64 65 } 66 67 } 68 69 }
如圖:
蒙
2017-07-21 14:40 週五