Entityframework.docs: Clarify that computed columns (and similar facets) must exist in the database schema

Created on 3 Aug 2018  Â·  4Comments  Â·  Source: dotnet/EntityFramework.Docs

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


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

area-entity-mapping

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.

All 4 comments

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MCcoder52 picture MCcoder52  Â·  3Comments

joakimriedel picture joakimriedel  Â·  3Comments

jaxidian picture jaxidian  Â·  4Comments

weitzhandler picture weitzhandler  Â·  4Comments

speciesunknown picture speciesunknown  Â·  3Comments