Add-Migration not adding column to existing table in Entity Framework Core

独自空忆成欢 提交于 2019-12-11 05:11:32

问题


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: Access to the path 'C:\Projects\PSM\Portal\src\Portal\Migrations\ApplicationDbContextModelSnapshot.cs' is denied.


回答1:


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



来源:https://stackoverflow.com/questions/43731604/add-migration-not-adding-column-to-existing-table-in-entity-framework-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!