【原创】EntityFramework Core 中使用 CodeFirst 模式时 PowerShell 版本问题及解决

2023-02-15,,,,

索引:

目录索引

一、描述:

在使用 Entity Framework Core 时,使用 CodeFirst 模式

在 VS 中的 PMC(nuget 包管理 控制台) 控制台界面使用如下命令:

           Install-Package Microsoft.EntityFrameworkCore.Tools

           Add-Migration Initial

           Update-Database

PMC bash

二、问题:

遇到的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 窗口,重新运行命令:

四、自动生成的代码:

  上下文代码

 using System;

 using System.Collections.Generic;

 using System.Linq;

 using System.Threading.Tasks;

 using Microsoft.EntityFrameworkCore;

 namespace MvcMovie.Models

 {

     public class MvcMovieContext : DbContext

     {

         public MvcMovieContext (DbContextOptions<MvcMovieContext> options)

             : base(options)

         {

         }

         public DbSet<MvcMovie.Models.Movie> Movie { get; set; }

     }

 }

C# code

  命令生成代码

 using System;

 using System.Collections.Generic;

 using Microsoft.EntityFrameworkCore.Migrations;

 using Microsoft.EntityFrameworkCore.Metadata;

 namespace MvcMovie.Migrations

 {

     public partial class Initial : Migration

     {

         protected override void Up(MigrationBuilder migrationBuilder)

         {

             migrationBuilder.CreateTable(

                 name: "Movie",

                 columns: table => new

                 {

                     ID = table.Column<int>(nullable: false)

                         .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),

                     Genre = table.Column<string>(nullable: true),

                     Price = table.Column<decimal>(nullable: false),

                     ReleaseDate = table.Column<DateTime>(nullable: false),

                     Title = table.Column<string>(nullable: true)

                 },

                 constraints: table =>

                 {

                     table.PrimaryKey("PK_Movie", x => x.ID);

                 });

         }

         protected override void Down(MigrationBuilder migrationBuilder)

         {

             migrationBuilder.DropTable(

                 name: "Movie");

         }

     }

 }

C# code

  如图:

                                         蒙

                                    2017-07-21 14:40  周五

原创】EntityFramework Core 中使用 CodeFirst 模式时 PowerShell 版本问题及解决的相关教程结束。

《【原创】EntityFramework Core 中使用 CodeFirst 模式时 PowerShell 版本问题及解决.doc》

下载本文的Word格式文档,以方便收藏与打印。