zoukankan      html  css  js  c++  java
  • 【原创】EntityFramework Core 中使用 CodeFirst 模式时 PowerShell 版本问题及解决

    索引:

    目录索引

    一、描述:

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

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

    1           Install-Package Microsoft.EntityFrameworkCore.Tools
    2 
    3           Add-Migration Initial
    4 
    5           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 窗口,重新运行命令:

     

    四、自动生成的代码:

      上下文代码

     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 }
    C# code

      命令生成代码

     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 }
    C# code

      如图:

                                             蒙

                                        2017-07-21 14:40  周五

  • 相关阅读:
    图片轮播切换
    php用get_meta_tags轻松获取网页的meta信息
    PHP创建桌面快捷方式实例
    php 获取网站根目录的写法
    php mkdir 创建多级目录实例代码
    php计算剩余时间的自定义函数
    php实现获取汉字的首字母实例
    PDO封装函数
    Struts动态表单(DynamicForm)
    [WPF]静态资源(StaticResource)和动态资源(DynamicResource)
  • 原文地址:https://www.cnblogs.com/Meng-NET/p/7217595.html
Copyright © 2011-2022 走看看