【发布时间】:2017-09-29 14:33:08
【问题描述】:
我正在尝试向数据库中的现有表添加一个新列,我正在将新字段指定到类中并运行 Add-Migration 命令,但每次它使用 CreateTable 方法而不是 AddColumn 为整个表创建迁移类。下面是类和生成的迁移类代码。
public class UserSetup
{
public int Id { get; set; }
public string Name { get; set; }
public bool Age{ get; set; } // New Field Added
}
但对于新字段,它正在为全表创建迁移类,如下所示:
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");
}
}
同样在 Add-Migration 中,它给了我以下错误,但即使是迁移类也正在创建中。
System.UnauthorizedAccessException:对路径“C:\Projects\PSM\Portal\src\Portal\Migrations\ApplicationDbContextModelSnapshot.cs”的访问被拒绝。
【问题讨论】:
标签: entity-framework database-migration