yv989c / BlazarTech.QueryableValues

This library allows you to efficiently compose an IEnumerable<T> in your Entity Framework Core queries when using the SQL Server database provider.
Other
90 stars 6 forks source link

EF.Property<T> throws exception with list used in AsQueryableValues() extension #46

Closed AhmedMSedeek closed 7 months ago

AhmedMSedeek commented 7 months ago

EF.Property throws exception with list used in AsQueryableValues() extension,

Details: EF Core Version: 8.0.1 Database Engine: SQL Server Exception: "The EF.Property method may only be used within Entity Framework LINQ queries" Note: Reverse engineering "scaffolding" was used to automatically generate the dbcontext

There're some cases where we cannot read properties directly and we need to use EF.Property instead, this extension does not work with AsQueryableValues list and throws the exception, I understand this exception is normally raised when client evaluation happens instead of server evaluation, so maybe when we use EF.Property along with AsQueryableValues it forces client evaluation for some reason, example with temporal tables below:

var modificationLogs = dbContext.Set<ObjectName>().TemporalAll().AsQueryable();
var properties = new List<string>() { "Name", "Code", "ValidFrom", "ValidTo" };
var query = from modificationLog1 in modificationLogs
            join modificationLog2 in modificationLogs on EF.Property<DateTime>(modificationLog1, "ValidTo") equals EF.Property<DateTime>(modificationLog2, "ValidFrom")
            from property in dbContext.AsQueryableValues(properties, true)
            select new ()
            {
                Date = EF.Property<DateTime>(modificationLog1, "ValidTo").ToString(),                     // this one works correctly,
                Field = property,                        // this one works correctly,
                From = EF.Property<object>(modificationLog1, "Name").ToString()                     // this one works correctly,
                // From = EF.Property<object>(modificationLog1, property).ToString()                     // this one "when enabled" does not work and throws the exception
            };

var items = await query.ToListAsync();                            // exception thrown here

Edit #1: Fixed Typo

yv989c commented 7 months ago

Hi @AhmedMSedeek, and thanks for posting this. Under the hood, this library builds on top of the FromSqlRaw API; therefore I'm afraid there's little—if anything—I can do about this scenario.

Have you confirmed that this is genuinely supported by EF? For instance, could you please try to reproduce this issue by consuming the values from your properties list directly from another entity in your DbContext? Does that work, or do you still get the same error?

Specifically, I'd like to know if the case where you dynamically specify the second parameter to EF.Property can be natively handled by EF.

AhmedMSedeek commented 7 months ago

Hello @yv989c, thank you a lot for your effort and interest, We tried you scenario by creating a table in the database that has only one varchar column with properties names list, and joining this table and calling the EF.Property with the value from this column did throw the same error, so the error is apparently related to Entity Framework core itself and not related to QueryableValues, Thanks a lot again, I'll create an issue on Entity Framework repo itself and see if they can resolve it!