2

I set an existing property decimal to decimal?:

    public decimal? TotalAmountTTC { get; set; }

Then I created a migration with add-migration, it generated me this :

        migrationBuilder.AlterColumn<decimal>(
            name: "c0003_total_amount_ttc",
            table: "t0003_transfer_request",
            type: "decimal(13,4)",
            nullable: true,
            oldClrType: typeof(decimal),
            oldType: "decimal(13,4)");

But after i execute update-database, the column is still not nullable:

enter image description here

When i run script-migration to check the SQL generated, we can see it clearly doesn't care about the fact my property is now nullable:

        ALTER TABLE "t0003_transfer_request" MODIFY "c0003_total_amount_ttc" decimal(13,4)
        /

Am I doing something wrong? Is this a bug?

I've tried to set the IsRequired(false) in the mapping, but same result.

         builder.Property(tr => tr.TotalAmountTTC).HasColumnName("c0003_total_amount_ttc").IsRequired(false);
Matthieu Charbonnier
  • 2,794
  • 25
  • 33
  • If everything else fails simple run `ALTER TABLE "t0003_transfer_request" MODIFY "c0003_total_amount_ttc" NULL;`. It is fairly good possible that such modification are not supported. What would you expect that the tool will do if you change a column from *nullable* to *not nullable*? – Marmite Bomber Apr 16 '20 at 12:35
  • @MarmiteBomber I don't want to mess with the whole CI/CD, and keep a single SQL query to update this column. – Matthieu Charbonnier Apr 16 '20 at 12:47
  • @MarmiteBomber I don't understand your question – Matthieu Charbonnier Apr 16 '20 at 12:48
  • The first was non ment as a *recomendation*, but as a *workaround*. The question concerns the *opposite scenario* - you have a nullable column and wants to change it in a *non-nullable* column. Here you must *mess* with the data - provide some *default* value before the column definition can be modified. Here *exists no simple tool solution*... – Marmite Bomber Apr 16 '20 at 14:23

1 Answers1

1

Ok, it looks like a hack to me, but I've found a way to make NULLABLE a column which already exists as NOT NULL :

You need to include the NULL in the datatype (in my case : decimal(13,4) NULL) :

    public void Configure(EntityTypeBuilder<TransferRequest> builder)
    {
        builder.Property(tr => tr.TotalAmountTTC).HasColumnType("decimal(13,4) NULL");
    }
Matthieu Charbonnier
  • 2,794
  • 25
  • 33