.Net Core - How to add SQL Column Constraint using EF Core

.net-core asp.net asp.net-core c# entity-framework-core

Question

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?

Right now I have a migrations folder with a InitialCreate, RenamedTable, and TableDefault migration. Before creating the TableDefault migration, I went into the DbContextModelSnapshot.cs file and added this to one of the columns

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. However, when I go to update-database, I notice the down method is actually applying those constraints and not the UP method?

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. 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.

1
0
4/9/2020 1:11:46 PM

Accepted Answer

The *Snapshot.cs is not meant to be modified. 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.

Your default constraint should be added on your migration file instead. You just need to put it into the Up method. You already have there defaultValueSql or defaultValue for that kind of thing. Don't modify the *Snapshot.cs file.

0
4/9/2020 1:23:16 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow