简体   繁体   English

调用 FromSql 在 IQueryable 上不起作用<tentity>从 Entity Framework 核心 2.2 迁移到 3.0 之后</tentity>

[英]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.我正在动态构建我的查询,我有一个接收可选的IOrderedQueryable<T>的方法。 This code used to work perfectly with EF Core 2.2 but stopped after the migration.此代码曾经与 EF Core 2.2 完美配合,但在迁移后停止。

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> .文档说FromSql已被替换为FromSqlRaw ,这是真的,这适用于DbSet但不适用于IQueryable<T> Any hints of how can I achieve this with EF Core 3.0?关于如何使用 EF Core 3.0 实现这一点的任何提示?

How i got it from doc in ef core 3.0 you can't do that.我是如何从 ef core 3.0 中的文档获得的,你不能这样做。

Before EF Core 3.0, the FromSql method could be specified anywhere in the query.在 EF Core 3.0 之前,可以在查询中的任何位置指定 FromSql 方法。

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<>.从 EF Core 3.0 开始,新的 FromSqlRaw 和 FromSqlInterpolated 方法(替换 FromSql)只能在查询根上指定,即直接在 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.在 DbSet 以外的任何位置指定 FromSql 没有附加意义或附加值,并且在某些情况下可能会导致歧义。

Mitigations缓解措施

FromSql invocations should be moved to be directly on the DbSet to which they apply. FromSql 调用应直接移动到它们应用的 DbSet 上。

You cannot use interpolation($) with FromSqlRaw() instead replace it with .FromSqlRaw("SELECT * FROM @tableName WHERE RowVersion > @p0", tablename,p0 })您不能将插值($)与 FromSqlRaw() 一起使用,而是将其替换为.FromSqlRaw("SELECT * FROM @tableName WHERE RowVersion > @p0", tablename,p0 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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