简体   繁体   English

.Net Core - 如何使用 EF Core 添加 SQL 列约束

[英].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.你已经有那种东西的defaultValueSqldefaultValue了。 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.

相关问题 EF Core 7:如何为 SQL 服务器添加 WITH(NOLOCK) - EF Core 7: How to add WITH(NOLOCK) for SQL Server 如何将EF核心迁移添加到.net核心2类库? - How to add EF core migrations to .net core 2 class library? EF Core 2、.NET CORE 2:如何使用 IQueryable 查询同一列的多个条件<t> ?</t> - EF Core 2, .NET CORE 2 :How do I query the same column for multiple conditions using IQueryable<T>? 如何将同一列添加到 EF Core 中的所有实体? - How to add the same column to all entities in EF Core? 如何使用 EF Core 处理更改 SQL 服务器数据库列类型 - How to handle changing SQL Server database column type with EF Core .NET Core EF 2.1 脚手架 - DatabaseFirst - 如何添加更多表 - .NET Core EF 2.1 Scaffolding - DatabaseFirst - How to add more tables 如何将 EF Core 与 Azure SQL 分片一起用于 .NET 5+? - How to use EF Core with Azure SQL Shards for .NET 5+? 如何运行 .NET 6 中的 SQL 脚本使用 EF Core Code 首次迁移 - How to run SQL scripts in .NET 6 using EF Core Code first migration 如何使用 .NET Core 中的 Entity Framework Core 在日期列中插入和更新 SQL Server DateTime - How to insert and update SQL Server DateTime in a date column using Entity Framework Core in .NET Core SQLite 外键约束使用 EF Core 添加到子集合失败 - SQLite foreign key constraint failed using EF Core to add to child collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM