Change float field to decimal

Asked

Viewed 299 times

1

I have a field, which in the model was like float, and that’s how you got saved, and you already have data in those columns. But what happens is that now I need 6 decimal places after the comma. Example: 10.023568

But with the float that’s not possible, or so far I haven’t been able to figure out any way to do it.

I thought of changing from float for decimal, but when I file for change:

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<decimal>(
            name: "ValorTotalPedido",
            table: "PedidoFornecedor",
            type: "decimal(10,6)",
            nullable: false,
            oldClrType: typeof(float));

        migrationBuilder.AlterColumn<decimal>(
            name: "ValorProdutos",
            table: "PedidoFornecedor",
            type: "decimal(10,6)",
            nullable: false,
            oldClrType: typeof(float));
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<float>(
            name: "ValorTotalPedido",
            table: "PedidoFornecedor",
            nullable: false,
            oldClrType: typeof(decimal),
            oldType: "decimal(10,6)");

        migrationBuilder.AlterColumn<float>(
            name: "ValorProdutos",
            table: "PedidoFornecedor",
            nullable: false,
            oldClrType: typeof(decimal),
            oldType: "decimal(10,6)");
    }

But when will I perform the update-database he returns me Arithmetic overflow error when converting real to Numeric data type.

I tried to change only mine ViewModel, but still saved with only two boxes 10.02.

In my Model the field is like this public float ValorTotalPedido { get; set; }

I tried to put it like this on ViewModel public decimal ValorTotalPedido { get; set; }

  • Already tried using parseFloat to convert integer to real

  • @luizgustavo already yes, and did not solve my problem.

  • Try to create an auxiliary column with the decimal type, transfer all value in it, and then delete the field of type float, save the update, then you can create the field with the decimal type and transfer the auxiliary field to it, and delete in the auxiliary field.

1 answer

0

The acceptance of changes to the DDL structure of the database by Migration will depend greatly on the database. If I’m not mistaken, structurally fields float are larger than double and such amendment shall not be permitted.

Recommended in this case is you manipulate your Migration to:

1 - Create a new temporary double field in the database;

2 - Updatar converting the current field to this new field;

3 - Remove the old field;

4 - Rename the temporary field to the old name (this possibility also depends on the database, if it does not work, consider creating the field with the old name and updatar back the information).

The migrations I did that required me to process in this way, and it makes sense considering the integrity of the information by the database.

I hope I helped. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.