添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello,

I am using a generic Repository to encapsulate the DbContext.
While using netcore2.2 with efcore 2.2.6 my statements are working fine.

Since the upgrade to 3.0.0 I can't use FirstOrDefault-/ SingleOrDefault-like Statements anymore.

Repository ( in .NET Standard 2.1 csproj )

public class Repository<T> : IRepository<T> where T : class, IEntity<int>, new()
        public virtual T GetSingle(Expression<Func<T, bool>> predicate,
                                                 params Expression<Func<T, object>>[] includeProperties)
            IQueryable<T> query = this.Context.Set<T>();
            query = includeProperties.Aggregate(query, (x, prop) => x.Include(prop));
            return query.FirstOrDefault(predicate.Compile()); /* Works as expected */
            return query.FirstOrDefault(predicate); /* Does not work */
        public virtual T GetSingle(int id)
            Func<T,bool> works = x => x.id == id;
            Expression<Func<T,bool>> doesNotWorks = x => x.Id == Id;
            return this.Context.Set<T>()
                       .FirstOrDefault(works);

TestClass ( .NET Core 3.0 )

[Fact]
public void GetbyId()
            /* throws as well */
            Expression<Func<PriceListElementHeader, bool>> query= x => x.Id == 12;
          /* Does not throw */
          Func<PriceListElementHeader, bool> query = x => x.Id == 12;          
            this.Context.Set<PriceListElementHeader>()
                .FirstOrDefault(query).Should().NotBeNull();
         /* Throws exception */
          new Repository<PriceListElementHeader>().GetSingle(x => x.Id == 12);
          new Repository<PriceListElementHeader>().GetSingle(12);
System.InvalidOperationException : The LINQ expression 'Where<PriceListElementHeader>(
    source: DbSet<PriceListElementHeader>, 
    predicate: (p) => p.Id == (Unhandled parameter: __id_0))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& )
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at VariantenManager.Data.Repositories.Repository`1.GetSingle(Int32 id) in C:\ShitJustGotReal\Backend\VariantenManager.Data\Repositories\Repository.cs:line 69
   at ApiTests.Controller.PriceListElementControllerTests.GetbyId() in C:\ShitJustGotReal\Tests\ApiTests\Controller\PriceListElementControllerTests.cs:line 31

Further technical details

EF Core version: 3.0.0 ( 3.1.0-preview3.19554.8 )
Database provider: Microsoft.EntityFrameworkCore.SqlServer 3.0.0 ( targeting SQL Server 12.0.6108.1 )
Target framework: .NET Core 3.0
Operating system: Win10-x64
IDE: Visual Studio 2019 16.3

It appears I cannot reproduce it anymore.
I recreated the Solution and.
As of now I just get this Error in my Test.

While using it in my API I just get a bunch of warnings

warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ([x].Id == __id_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ([x].Id == __id_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Take(1)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ([x].Id == __id_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Take(1)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join AnonymousObject _x in {from PriceListElementHeader x in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[VariantenManager.Model.Entities.PriceListElementHeader]) where ([x].Id == __id_0) orderby EF.Propert
y(?[x]?, "PriceListElementId") asc select new AnonymousObject(new [] {Convert(EF.Property(?[x]?, "PriceListElementId"), Object)}) => Take(1)} on Property([x.Configs], "PriceListElementId") equals Convert([_x].GetValue(0), Nullable`1)' could not be translate
d and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [_x].GetValue(0) asc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ([x].Id == __id_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Take(1)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ([x].Id == __id_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Take(1)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join AnonymousObject _x in {from PriceListElementHeader x in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[VariantenManager.Model.Entities.PriceListElementHeader]) where ([x].Id == __id_0) orderby EF.Propert
y(?[x]?, "PriceListElementId") asc select new AnonymousObject(new [] {Convert(EF.Property(?[x]?, "PriceListElementId"), Object)}) => Take(1)} on Property([x.Prices], "PriceListElementId") equals Convert([_x].GetValue(0), Nullable`1)' could not be translated
 and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [_x].GetValue(0) asc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ([x].Id == __id_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Take(1)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where ([x].Id == __id_0)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Take(1)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'join AnonymousObject _x in {from PriceListElementHeader x in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[VariantenManager.Model.Entities.PriceListElementHeader]) where ([x].Id == __id_0) orderby EF.Propert
y(?[x]?, "PriceListElementId") asc select new AnonymousObject(new [] {Convert(EF.Property(?[x]?, "PriceListElementId"), Object)}) => Take(1)} on Property([x.Placeholders], "PriceListElementId") equals Convert([_x].GetValue(0), Nullable`1)' could not be tran
slated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'orderby [_x].GetValue(0) asc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'FirstOrDefault()' could not be translated and will be evaluated locally.

I put my ReproductionCode into a new Repository.

I don't know why I can reproduce it only in the Old Solution and only in the TestRuns.

As far as I am concerned this Issue can be closed