Efcore: Migrations: Attempt better ordering when adding computed columns

Created on 25 Oct 2016  路  4Comments  路  Source: dotnet/efcore

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

closed-fixed good first issue help wanted type-bug

All 4 comments

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.

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.

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

Fixed in 1b1abb1

Was this page helpful?
0 / 5 - 0 ratings