Skip to content

Migrations does not properly work with calculated properties #12726

Closed as not planned

Description

@iamkarlson

I have a computed column for remaining amount. It's calculated as total amount minus amount. So, it has public getter and unavailable setter declared as

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public virtual decimal RemainingAmount {
  get => TotalAmount - Amount;
  internal set { }
}

So, when I'm adding a migration, without any additional manipulation it gets me an SQL error of the dependency this computed column on altering column.

I tried to manually add dropping and creation:

            migrationBuilder.DropColumn(
                name: "RemainingAmount",
                table: "Orders");

            migrationBuilder.AlterColumn<decimal>(
                name: "Amount",
                table: "Orders",
                type: "DECIMAL(18,3)",
                nullable: false,
                oldClrType: typeof(decimal));

            migrationBuilder.AddColumn<decimal>(
                name: "RemainingAmount",
                table: "Orders",
                type: "DECIMAL(19,3)",
                nullable: false,
                computedColumnSql: "[TotalAmount] - [Amount]");

SQL profiled doesn't indicate that EF is trying to drop that column. Do I miss anything? How do I have to instruct EF to drop that column before altering main one?

Further technical details

EF Core version: 2.1.1

Activity

  1. changed the title [-]Migrations does not properly work with migrations[/-] [+]Migrations does not properly work with calculated properties[/+] on Jul 19, 2018
  2. added this to the milestone on Jul 20, 2018
  3. modified the milestones: , on Sep 11, 2018
  4. modified the milestones: , 3.0.0 on Oct 1, 2018
  5. 6 remaining items

  6. modified the milestones: Backlog, on Aug 21, 2019
  7. bricelam commented on Oct 9, 2019

    @bricelam
    Contributor

    I think fixing #6853 was the most we could do for computed columns without actually parsing the SQL (a complexity we desperately wish to avoid).

    Manually rebuilding the computed column in the migration is the correct way to handle this. I was able to make this work locally. Hard to say what went wrong. Perhaps you forgot to save the migration after editing it?

  8. removed this from the milestone on Oct 9, 2019
  9. iamkarlson commented on Oct 9, 2019

    @iamkarlson
    Author

    Hi @bricelam
    Thank you for your efforts! I don't think that I can recall anything from what was happening more than a year ago. However, I do remember that I used a workaround with a direct SQL script. Also, I think it's a good idea to close this ticket since I have no usage of it anymore. Please reopen it if you think that would be appropriate.

  10. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions