could not be translated and will be evaluated locally. EF CORE

时间:2018-06-04 17:40:17

标签: c# entity-framework .net-core ef-core-2.0

The SQL WHERE clause is not being generated when using properties of my base class.

The following example is a basic version of the problem:

Exception message

Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'where (new PackageViewModel() {CreatedByUserName = [p.CreatedBy].UserName, Id = [p].Id}.CreatedByUserName == "Ivan")' could not be translated and will be evaluated locally.

public class BaseViewModel
{
    public String CreatedByUserName { get; set; }
}

public class PackageViewModel : BaseViewModel
{
    public Guid Id { get; set; }
}

public class myTest
{
    public void Test()
    {   
       var query = _dbContext.Set<Package>().Select(p => new PackageViewModel()
        {
            CreatedByUserName = p.CreatedBy.UserName,
            Id = p.Id
        });

        var param = Expression.Parameter(typeof(PackageViewModel), "p");
        var exp = Expression.Lambda<Func<PackageViewModel, bool>>(
            Expression.Equal(
                Expression.Property(param, "CreatedByUserName"),
                Expression.Constant("Ivan", typeof(String))
            ), param
        );

        var result = query.Where(exp).ToList();
    }
}

If I try with:

var param = Expression.Parameter(typeof(BaseViewModel), "p");

It works, but then it doesn't work with other properties like Id, because they doesn't exist in the base class.

The problem is that in my final version I have some dynamic where’s over properties that are in my main class and others that are in my base class; which will result in something like this : ( p => p.Id == "…" && p.CreatedByUserName == "…")

Questions

Is this a bug?

Why do properties that are in my base class only evaluate locally?

Is there a workaround?

Is the solution to copy all properties from my base class to all my main classes?

Further technical details

EF Core version: 2.0.3

Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL 2.0.1

Operating system:

IDE: Visual Studio 2017 15.7.2

0 个答案:

没有答案