I was trying to use HasComputedColumnSql without migrations enabled, it didn't work. Found this SO post https://stackoverflow.com/questions/39306020/computed-column-in-ef-core-incorrect-mapping
which says that computed columns are only supported when migrations are enabled.
In my case HasComputedColumnSql is ignored and actual column name is used instead.
IMHO it makes sense to update documentation and make it clear when this option is supported
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@VirMaker Can you provide some more information as to what you mean by "actual column name is used instead?" (I ask because, EF will always use the column name--that is, HasComputedColumnSql does not mean that the column does not exist in the database, it means that it is a computed column in the database, and I'm wondering if this is confusing.)
@ajcvickers I think I expected this column to affect SELECT and not CREATE. For example I expected the model below
modelBuilder.Entity
.Propery(p => p.LastModified)
.HasComputedColumnSql("GetUtcDate()");
to generate SELECT GetUtcDate() as LastModified FROM ... Instead it executes SELECT LastModified FROM .... I guess HasComputedColumnSql is designed for generation of SQL COMPUTED columns during CREATE, while I expected it to change SELECT statement
I hope it helps.
@ajcvickers completely agree, this feature is very misleading and will lead many to waste time believing it can be used as a SELECT expression. Case in point JSON_VALUE(...)
Is there any way to generate something like
SELECT CONVERT(DATETIME, [Date]) + CONVERT(DATETIME, [Time]) AS [DateTime] From SomeDateTimeTable
Was hoping this mapping will work
C#
builder.Property(x => x.DateTime)
.HasComputedColumnSql("CONVERT(DATETIME, [Date]) + CONVERT(DATETIME, [Time])");
It will be very helpful if there is a way to select multiple fields or has computed column for one single property
Most helpful comment
@ajcvickers I think I expected this column to affect SELECT and not CREATE. For example I expected the model below()
modelBuilder.Entity
.Propery(p => p.LastModified)
.HasComputedColumnSql("GetUtcDate()");
to generate SELECT GetUtcDate() as LastModified FROM ... Instead it executes SELECT LastModified FROM .... I guess HasComputedColumnSql is designed for generation of SQL COMPUTED columns during CREATE, while I expected it to change SELECT statement
I hope it helps.