Translation of Projectable property fails
See original GitHub issueHi,
What can be wrong in my code if translation of this simple projectable property fails (version is the latest 2.2.0, platform is .NET 6 with EF Core 6)?
query: var q2 = context.Students.Where(x => x.IsPaid);
Exception message when translate this query using q2.ToQueryString()
:
The LINQ expression ‘DbSet<Student>()
.Where(s => !(s.IsDeleted))
.Where(s => s.IsPaid)’ could not be translated. Additional information: Translation of member ‘IsPaid’ on entity type ‘Student’ failed. This commonly occurs when the specified member is unmapped. 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’.
Student class has some real properties and also this one in a partial class:
[Projectable] public bool IsPaid => PaymentMethod == 1;
Configuration of dbcontext:
.AddInterceptors(provider.GetRequiredService<FillAuditPropertiesInterceptor>())
.UseSqlServer(configuration.GetConnectionString(nameof(AppDbContext)))
.ConfigureWarnings(x => x.Ignore(SqlServerEventId.SavepointsDisabledBecauseOfMARS)) //
.UseInternalServiceProvider(provider)
.UseProjectables(o => o.CompatibilityMode(EntityFrameworkCore.Projectables.Infrastructure.CompatibilityMode.Full)), ServiceLifetime.Scoped);
Content of generated file:
using AB.KELLO.TANK2.Features.Infrastructure.Database.Interfaces;
using EntityFrameworkCore.Projectables;
using AB.KELLO.TANK2.Features.Domain.Entities;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class AB_KELLO_TANK2_Features_Domain_Entities_Student_IsPaid
{
public static System.Linq.Expressions.Expression<System.Func<global::AB.KELLO.TANK2.Features.Domain.Entities.Student, bool>> Expression()
{
return (global::AB.KELLO.TANK2.Features.Domain.Entities.Student @this) =>
@this.PaymentMethod == 1;
}
}
}
Query filter is used, that causes this part of the query: .Where(s => !(s.IsDeleted))
Context is prepared for using Projectables (UseProjectables() is called) , attribute is used on property…
Do you have any idea what is wrong here?
Issue Analytics
- State:
- Created a year ago
- Comments:12 (7 by maintainers)
Top GitHub Comments
Hi,
If we use the given values (“Student” and “AB_KELLO_TANK2_Features_Domain_Entities_Student_IsPaid”) this query will fail always.
Full name of a type contains the name of the namespace also.
The namespace of the generated class is
EntityFrameworkCore.Projectables.Generated
(from the generated file).You may rewrite the Where part of the query llike this:
.Where (x => x.Namespace == "EntityFrameworkCore.Projectables.Generated" && x.Name == generatedContainingTypeName )
In this way the method named Expression() will be the result of the query.
Maybe it helps…
Going to close this as the issue has become stale, feel free to reopen if this is still an issue