【发布时间】:2020-07-22 02:54:40
【问题描述】:
在 .net core 3.1 Web 应用程序中,我应该编辑哪个文件以向 EF 创建的表添加 DEFAULT 约束?
现在我有一个迁移文件夹,其中包含 InitialCreate、RenamedTable 和 TableDefault 迁移。在创建 TableDefault 迁移之前,我进入了 DbContextModelSnapshot.cs 文件并将其添加到其中一列
b.Property<string>("EmergencyChange")
.IsRequired()
.HasColumnType("nvarchar(max)")
.HasDefaultValue("False"); //Default constraint I added
然后我创建了一个新的迁移并将其命名为 TableDefault。但是,当我转到 update-database 时,我注意到 down 方法实际上是在应用这些约束而不是 UP 方法?
public partial class TableDefaults : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Submitter",
table: "tblChanges",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)",
oldDefaultValueSql: "SUSER_Name()");
migrationBuilder.AlterColumn<string>(
name: "EmergencyChange",
table: "tblChanges",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)",
oldDefaultValue: "False");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Submitter",
table: "tblChanges",
type: "nvarchar(max)",
nullable: false,
defaultValueSql: "SUSER_Name()",
oldClrType: typeof(string));
migrationBuilder.AlterColumn<string>(
name: "EmergencyChange",
table: "tblChanges",
type: "nvarchar(max)",
nullable: false,
defaultValue: "False",
oldClrType: typeof(string));
}
}
所以当我运行 UP 方法时,它基本上什么都不做,但如果我恢复到第二次迁移,它会运行 down 方法,现在突然应用了约束。那么,我做错了什么?我可以交换 UP/DOWN 方法,但我很确定我做的事情根本不正确。
【问题讨论】:
标签: c# asp.net asp.net-core .net-core entity-framework-core