How do I change a null string to non-null int in Entity Framework 2 Core?

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

Question

This is short: I have mistakenly set a property string when it should've been int. There are no constraints as this int may be optional. Even though I could make the int nullable, I rather have it a default value of zero.

Then when I try to run migrations:

Cannot insert the value NULL into column 'Edad', table 'EduPlaTools.dbo.Profesor'; column does not allow nulls. UPDATE fails. The statement has been terminated.

Which makes sense. What it's doing is updating the fields, and since many fields had "NULL", it's getting inserted. Is there a way to map those NULLs into zeroes automatically? Or do I need to do an SQL Statement?

Here's the migrationBuilder code:

   migrationBuilder.AlterColumn<int>(
                name: "Edad",
                table: "Profesor",
                nullable: false,
                oldClrType: typeof(string),
                oldNullable: true);
1
1
12/5/2018 7:56:30 PM

Popular Answer

Oh no. I need to apologize. So dumb for not realizing this, Entity Framework apparently scaffolded other table with a similar property Profesor, when in fact it was Estudiante that needed change.

        migrationBuilder.AlterColumn<int>(
            name: "Edad",
            table: "Estudiante",
            nullable: false,
            oldClrType: typeof(string),
            oldNullable: true,
            defaultValue: 0);

Now everything is working! Thanks everybody!

1
12/5/2018 8:41:58 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