索引:
一、描述:
在使用 Entity Framework Core 时,使用 CodeFirst 模式,
在 VS 中的 PMC(nuget 包管理 控制台) 控制台界面使用如下命令:
1 Install-Package Microsoft.EntityFrameworkCore.Tools 2 3 Add-Migration Initial 4 5 Update-Database
二、问题:
遇到的PowerShell 版本问题,如下:
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.
三、解决方法:
1) 下载4.0版本
https://www.microsoft.com/zh-CN/download/details.aspx?id=40855
2) 安装下载完成的包:
3) 打开VS PMC 窗口,重新运行命令:
四、自动生成的代码:
上下文代码
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 周五