Base solution for your next web application
Open Closed

Issue: Linq / Entity Framework Core | String.Format #11894


User avatar
0
AuroraBMS created

Good day,

Version 12.4 Angular Core

This has been something that has been haunting us for a while now and I've been looking for a resolution for a very long time but I can't find anything worth mentioning in terms of string.Format usage that is generate by power tools.

The below is the structure that we are using when generating some navigation properties:

It generates this as part of the query:

.WhereIf(!string.IsNullOrWhiteSpace(input.TaxCodeSetupDisplayPropertyFilter), e => string.Format("{0} ( {1} )", e.TaxCodeSetupFk == null || e.TaxCodeSetupFk.TaxGroupCode == null ? "" : e.TaxCodeSetupFk.TaxGroupCode.ToString() , e.TaxCodeSetupFk == null || e.TaxCodeSetupFk.TaxGroupName == null ? "" : e.TaxCodeSetupFk.TaxGroupName.ToString() ) == input.TaxCodeSetupDisplayPropertyFilter)

As this has been part of the general ASP.NET ZERO releases for a while now I assume I am just missing some kind of package that allows for the translation to actually succeed but I am completely in the dark as to what it could be as string.format is a known linq to entity framework translation issue.

Below the error that is thrown: System.InvalidOperationException: The LINQ expression 'DbSet() .Where(t => __ef_filter__p_0 || !(((ISoftDelete)t).IsDeleted) && __ef_filter__p_1 || ((IMayHaveTenant)t).TenantId == __ef_filter__CurrentTenantId_2) .Where(t => string.Format( format: "{0} ( {1} )", arg0: t.TaxGroupCode, arg1: t.TaxGroupName).Contains(__input_Filter_0))' could not be translated. Additional information: Translation of method 'string.Format' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information. at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.g__CheckTranslated|15_0(ShapedQueryExpression translated, <&gt;c__DisplayClass15_0&) at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKe...

As this is out of the box functionality in a released version I would assume that the base generated code does not require customizations such as changing to base generated code, ref to: Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

any assistance regarding this would be greatly appreciated.


1 Answer(s)