Skip to content

Not able to use Computed/Generated columns. #25207

@chravikishore

Description

@chravikishore

Not able to use Computed/Generated columns.

I am using the HasComputedColumnSql extension like below

entityTypeBuilder
               .Property(e => e.ConversionRate)
               .HasComputedColumnSql("(BaseUnitQuantity * 1.0)/Quantity")
               .IsRequired();

and the migration generated the script successfully as below

migrationBuilder.CreateTable(
                name: "MyTable",
                columns: table => new
                {
                    ...............
                    ConversionRate = table.Column<decimal>(nullable: false, computedColumnSql: "(BaseUnitQuantity * 1.0)/Quantity")
                },

But when I update the database it is giving following error

Failed executing DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "MyTable" (
    "ColID" INTEGER NOT NULL CONSTRAINT "PK_MyTable" PRIMARY KEY AUTOINCREMENT,
    "Col1" INTEGER NOT NULL,
    "Quantity" INTEGER NOT NULL,
    "Col2" INTEGER NOT NULL,
    "BaseUnitQuantity" INTEGER NOT NULL,
    "ConversionRate" AS (CAST(BaseUnitQuantity AS DECIMAL)/Quantity),
...........
);
Disposing transaction.
Closing connection to database 'main' on server '.......\bin\Debug\netcoreapp3.1\.....db'.
Closed connection to database 'main' on server '......\bin\Debug\netcoreapp3.1\.....db'.
'....DataContext' disposed.
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'near "AS": syntax error'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext()
   at Microsoft.Data.Sqlite.SqliteCommand.GetStatements(Stopwatch timer)+MoveNext()
   at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
SQLite Error 1: 'near "AS": syntax error'.

EF Core version: 5.0.7
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: netcoreapp3.1
Operating system: Windows
IDE: Visual Studio 2019 16.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions