Skip to content

Migrations: Attempt better ordering when adding computed columns #6853

Closed

Description

@sharok

Steps to reproduce

I have added some additional fields to ApplicationUser.

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }      
}

The FullName is computed property. So, in the context, I have:

 protected override void OnModelCreating(ModelBuilder builder)
 {
     builder.Entity<ApplicationUser>()
     .Property(p => p.FullName)
     .HasComputedColumnSql("[FirstName] + ' ' + [LastName]");

     base.OnModelCreating(builder);          
 }

The issue

In result, I get the following migration file:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
            name: "FirstName",
            table: "AspNetUsers",
            nullable: true);

   migrationBuilder.AddColumn<string>(
            name: "FullName",
            table: "AspNetUsers",
            nullable: true,
            computedColumnSql: "[FirstName] + ' ' + [LastName]");

    migrationBuilder.AddColumn<string>(
           name: "LastName",
           table: "AspNetUsers",
           nullable: true);
}

As you can see, the computed proeprty is added before LastName and I get error:
Invalid column name 'LastName'.

After moving the creation of the computed property to the end, updating database finished wuthout error.

Further technical details

EF Core version: 1.0.1
Operating system: Windows 10
Visual Studio version: Visual Studio 2015 Community

Activity

  1. changed the title [-]Wrong order in migration file.[/-] [+]Wrong order in a migration file.[/+] on Oct 25, 2016
  2. rowanmiller commented on Oct 25, 2016

    @rowanmiller
    Contributor

    The string given in HasComputedColumnSql is just a transparent string to us and we don't attempt to do any parsing etc. This is one of the scenarios where you would just edit the scaffolded migration based on what you know about the schema (in this case re-ordering the operations). We don't want to go down the route of trying to parse and reason about the computed column dependencies.

  3. sharok commented on Oct 26, 2016

    @sharok
    Author

    I see. But, I think, it would be better if you just make some simple analysis. For example, put all computed properties to the end.

  4. added this to the milestone on Oct 26, 2016
  5. added
    help wanted
    on Oct 26, 2016
  6. changed the title [-]Wrong order in a migration file.[/-] [+]Migrations: Attempt better ordering when adding computed columns[/+] on Oct 26, 2016
  7. rowanmiller commented on Oct 26, 2016

    @rowanmiller
    Contributor

    We'll take a look at the idea of adding computed columns after adding other columns.

  8. added a commit that references this issue on Oct 29, 2016
    e1bbc64
  9. added a commit that references this issue on Oct 31, 2016
    1b1abb1
  10. AndriySvyryd commented on Oct 31, 2016

    @AndriySvyryd
    Member

    Fixed in 1b1abb1

  11. added a commit that references this issue on Nov 8, 2016
    b310ce4
  12. added
    good first issue
    and removed
    good first issue
    on May 31, 2019
  13. modified the milestones: , 2.0.0 on Oct 15, 2022
  14. 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

No one assigned

    Labels

    good first issuehelp wanted

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions