Skip to content

Computed columns migration error #1621

@heggi

Description

@heggi

In EFCore 3.1 I did:

migrationBuilder.CreateTable(
      name: "Worker",
      columns: t => new {
          LastName = t.Column<string>(type: "varchar(64)"),
          FirstName = t.Column<string>(type: "varchar(64)"),
          MiddleName = t.Column<string>(type: "varchar(64)"),
          FullName = t.Column<string>(type: "varchar(128)",
              computedColumnSql: @"trim(""LastName"" || ' ' || ""FirstName"" || ' ' || ""MiddleName"")"),
      }
  )

In EFCore 5.0 this code got error System.NotSupportedException: Generated columns currently must be stored, specify IsStoredComputedColumn in your context's OnModelCreating

I added to DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Worker>()
        .Property(m => m.FullName)
        .HasComputedColumnSql(@"trim(""LastName"" || ' ' || ""FirstName"" || ' ' || ""MiddleName"")", stored: true);
}

But got the same error

If I remove computedColumnSql: "..." from migration code, I get general column.

My Entity:

public class Worker
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string FullName { get; set; }
}

Where is the error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions