Calling FromSql Not working on IQueryable after migration from Entity Framework core 2.2 to 3.0

c# entity-framework-core entity-framework-core-3.0

Question

I'm building my query dynamically, I have a method that receives an IOrderedQueryable<T> which is optional. This code used to work perfectly with EF Core 2.2 but stopped after the migration.

IQueryable<T> query;

if (spec.OrderedQueryable == null)
{
    query = DbContext.Set<T>()
                     .FromSqlRaw($"SELECT * FROM {tableName} WHERE RowVersion > @p0", new[] { lastRowVersion });
}
else
{
    query = spec.OrderedQueryable;
    query = query.FromSql($"SELECT * FROM {tableName} WHERE RowVersion > @p0", new[] { lastRowVersion });
}

The docs say that FromSql has been replaced with FromSqlRaw, that is true this works on DbSet but not on IQueryable<T>. Any hints of how can I achieve this with EF Core 3.0?

1
1
10/6/2019 6:44:39 AM

Popular Answer

How i got it from doc in ef core 3.0 you can't do that.

Before EF Core 3.0, the FromSql method could be specified anywhere in the query.

New behavior

Starting with EF Core 3.0, the new FromSqlRaw and FromSqlInterpolated methods (which replace FromSql) can only be specified on query roots, i.e. directly on the DbSet<>. Attempting to specify them anywhere else will result in a compilation error.

Why

Specifying FromSql anywhere other than on a DbSet had no added meaning or added value, and could cause ambiguity in certain scenarios.

Mitigations

FromSql invocations should be moved to be directly on the DbSet to which they apply.

0
10/6/2019 7:13:51 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow