简体   繁体   中英

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

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?

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, ie 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.

You cannot use interpolation($) with FromSqlRaw() instead replace it with .FromSqlRaw("SELECT * FROM @tableName WHERE RowVersion > @p0", tablename,p0 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM