Add-Migration不将列添加到Entity Framework Core中的现有表 [英] Add-Migration not adding column to existing table in Entity Framework Core

查看:163
本文介绍了Add-Migration不将列添加到Entity Framework Core中的现有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向数据库中的现有表添加新列,我在类中指定新字段并运行Add-Migration命令,但是每次它使用CreateTable方法而不是AddColumn为整个表创建迁移类时。以下是类和生成的迁移类代码。

I am trying to add a new column to my existing table in database, i am specifying new field into class and running Add-Migration command but everytime it is creating a migration class for whole table with CreateTable method instead of AddColumn. Below is the class and generated migration class codes.

public class UserSetup
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool Age{ get; set; } // New Field Added

    }

但是对于新字段为全表创建迁移类,如下所示:

But for new field it is creating migration class for full table as shown below:

public partial class AddUserSetup_1 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "UserSetup",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Name= table.Column<string>(nullable: false),
                    Age= table.Column<int>(nullable: false),
                 },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UserSetup", x => x.Id);
                });

       }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "UserSetup");
        }
    }

在添加迁移中,它还为我提供了以下内容错误,但甚至正在创建迁移类。

Also in Add-Migration it is giving me the following error but even migration class is getting created.

System.UnauthorizedAccessException:访问路径'C:\Projects\PSM\Portal\src \Portal\Migrations\ApplicationDbContextModelSnapshot.cs'被拒绝。

推荐答案

如果使用的是TFS,确保签出以编辑 ApplicationDbContextModelSnapshot.cs文件。它将正常工作!

If you are using TFS make sure to checkout for edit your 'ApplicationDbContextModelSnapshot.cs' file. It will work just fine!

这篇关于Add-Migration不将列添加到Entity Framework Core中的现有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆