[英].Net Core - How to add SQL Column Constraint using EF Core
In a .net core 3.1 web application, what file am I suppose to edit to add a DEFAULT constraint to a table that is being created by EF?在 .net 核心 3.1 web 应用程序中,我应该编辑什么文件以向 EF 创建的表添加 DEFAULT 约束?
Right now I have a migrations folder with a InitialCreate, RenamedTable, and TableDefault migration.现在我有一个迁移文件夹,其中包含 InitialCreate、RenamedTable 和 TableDefault 迁移。 Before creating the TableDefault migration, I went into the DbContextModelSnapshot.cs file and added this to one of the columns
在创建 TableDefault 迁移之前,我进入 DbContextModelSnapshot.cs 文件并将其添加到其中一列
b.Property<string>("EmergencyChange")
.IsRequired()
.HasColumnType("nvarchar(max)")
.HasDefaultValue("False"); //Default constraint I added
I then created a new migration and gave it the name TableDefault.然后我创建了一个新的迁移并将其命名为 TableDefault。 However, when I go to update-database, I notice the down method is actually applying those constraints and not the UP method?
但是,当我 go 更新数据库时,我注意到 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));
}
}
So when I run the UP method, it basically does nothing, but if I revert back to the 2nd migration, it runs the down method and now all of a sudden the constraint is applied.所以当我运行 UP 方法时,它基本上什么都不做,但是如果我恢复到第二次迁移,它会运行 down 方法,现在突然应用了约束。 So, what am I doing wrong?
那么,我做错了什么? I could just swap the UP/DOWN method, but I am pretty certain I am doing something fundamentally incorrect.
我可以交换 UP/DOWN 方法,但我很确定我在做一些根本不正确的事情。
It is always better to add Constraints on Modelbuilder在 Modelbuilder 上添加约束总是更好
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>().Property(b => b.Created).HasDefaultValueSql("getdate()"); }
The *Snapshot.cs
is not meant to be modified. *Snapshot.cs
不能被修改。 At the top of the file we have this:在文件的顶部,我们有这个:
// <auto-generated />
It is always auto-generated and updated by EF Core when you add a new migration file.添加新迁移文件时,它始终由 EF Core 自动生成和更新。
Your default constraint should be added on your migration file instead.您的默认约束应该添加到您的迁移文件中。 You just need to put it into the
Up
method.您只需将其放入
Up
方法中。 You already have there defaultValueSql
or defaultValue
for that kind of thing.你已经有那种东西的
defaultValueSql
或defaultValue
了。 Don't modify the *Snapshot.cs
file.不要修改
*Snapshot.cs
文件。
You could, As it is entity configuration rather than migration, all your entity configuration are in one place您可以,因为它是实体配置而不是迁移,所以您的所有实体配置都在一个地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.