We are using Entity Framework Core 3 with SqlServer Database. Business program needs to create many columns which are not in the Database, due to storage, high querying cost etc. Currently, the team is Copying the whole Database Layer, and Creating whole another layer adding computed members in new entities. Currently taking database layer and applying AutoMapper to new layer. For some reason, this does not seem like optimal method.
In this example, we require computed members called
FullName => FirstName + LastName
AccountValue => Quantity * StockPrice
Entity Framework 3 does not allow Client Side Evaluation anymore, https://devblogs.microsoft.com/dotnet/announcing-ef-core-3-0-and-ef-6-3-general-availability/ so curious what is standardized way for computed members in Entity Framework Core 3?
Reading this article, curious wondering what is up to date, and can be used? Or does Entity Framework Core 3 Offer New Syntax?
https://daveaglick.com/posts/computed-properties-and-entity-framework
1) We could Materialize the entities. Seems okay, however this forces developer to remember utilize ToList(), had issues where developers forget, causing long db scanning queries, or clientside evaluation caused error.
var result = ctx.Customers
.ToList()
.Select(c => new
{
FullName = c.FullName,
AccountValue = c.AccountValue
});
2) Create Queryable Extension. This only extracts the computed columns, or forces developers to create computed members all in one class (breaks SRP single responsibility idea). Unless there is an alternative modification which address this. This also brings composition chain issues, and possible performance problems like option 1.
public static IQueryable<CustomerData> SelectCustomerData(this IQueryable<Customer> customers) { return customers.Select(c => new CustomerData {
FullName = c.FirstName + " " + c.LastName,
AccountValue = c.Holdings.Sum(h => h.Quantity * h.Stock.Price) }); }
3) Expression Projection, does not allow assignment in Select without Linq Expression Project. Company does not allow this third party tool, unless built from Microsoft.
public readonly Expression<Func<Customer, decimal>> AccountValueExpression = c => c.Holdings.Sum(h => h.Quantity * h.Stock.Price);
Or does Entity Framework Core 3 offer newer syntax?
Solution needs to be where, (a) person can extract some or All of the existing members of original DBEntity, (b) and some or all of New Members,
Example, need FirstName (Existing) and AccountValue (New Member)
Or FullName, FirstName, LastName, StockPrice,
Or Everything, FirstName, LastName, FullName ,Quantity, StockPrice, AccountValue, etc, etc
Any mix or match from entities.
Actually migrating from 2.2 to Core 3, however 2.2 has ClientSide Evaluation Disabled. Cannot utilize third party tools, like Linq.Translations, or DelegateCompiler unless they are created from Microsoft vendor .
Prefer not to use SqlServer Computed columns, as we are relying on DBA team. Additionally there are more intricate calculations.