Closed Evengard closed 2 weeks ago
No way. You have to project all fields.
With custom projection (as in your case) you can remove all Includes
, they will be ignored.
Ok, let me rephrase the question - is there a way to define a database-generated field with linq syntax with help of linq2db?
To define computed column, ExpressionMethodAttribute
attribute could be used. E.g. using fluent mapping:
var builder = new FluentMappingBuilder();
builder.Entity<Material>()
.Property(e => e.LinkedFrom)
.HasAttribute(new ExpressionMethodAttribute((IDataContext db, Material e)
=> db.GetTable<Material>()
.Where(r => Sql.Ext.PostgreSQL().ValueIsEqualToAny(e.MaterialId, r.Linked))
.ArrayAggregate(r => r.MaterialId, Sql.AggregateModifier.Distinct).ToValue()));
db.AddMappingSchema(builder.Build().MappingSchema);
but it is not possible to attach it only to specific queries - all non-projected entity queries will select this column. Only option to use it with specific queries is to add mapping schema configuration to contexts that execute those queries.
Hello!
I have a following data model (using PgSql array data type):
Now, let's assume we have 2 materials with ID 1 and 2. The Material 1 have Material 2 in his array
Linked
. Now, I need to have the Material 2 populate theLinkedFrom
array (database-generated) with the ID 1 (inferred from the fact that the Material 1 have Material 2 in hisLinked
array). Now, EF Core does have a.HasComputedColumnSql("someSql", stored: false);
for a similar case, but I want to avoid using plain SQL at all costs and instead was hoping to use Linq2Db on query-time to populate it somehow. Now, probably I could do it with something like this (tentative code):But the actual Material object is actually a lot bigger than theese 3 fields, and recreating it all by hand seems to be way too cumbersome.
I probably could just return it as
new { Material = grp.Key, LinkedFrom = grp.ArrayAggregate(m => m.MaterialId, Sql.AggregateModifier.Distinct).ToValue() }
and then just patching it up with a simple foreach after the query as well, but that also seems a bit of a workaround than a solution. Also I am applying a bunch ofInclude
s on it, so that makes that cumbersome too.Is there a way to somehow specify only one column to "patch" (a bit like
.Set()
for updating multiple queries, but at query-time only)?