I have a table. Let say:
TableA
- Id
- ColumnA
The C# entity is defined with the properties Id, ColumnA, int ColumnB
.
I would like that in normal cases, the ColumnB
is set to -1 and when executing a RawSQL I can have this column in the SELECT clause that would fill the property.
Raw SQL
SELECT *, 123 AS ColumnB FROM TableA
Attempts
I tried to used NotMapped
attribute on ColumnB
property ==> No issue for the normal case loading, but using the RawSQL having this column added/generated, the value is not in the property.
I tried to used DatabaseGenerated(DatabaseGeneratedOption.Computed)
on ColumnB
property ==> The RawSQL works, but now trying to load an entity without RawSQL crashes.
I tried using:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TableA>().Property(m => m.ColumnB).HasComputedColumnSql("-1");
}
But same result as previous attempt.
Without creating a new class that overrides the TableA class and that includes the ColumnB property, would there be a way to obtain this behavior?