Basic Type-Generic EF Core 3.1 -> SQL Server Where Clause cannot be translated

1

Using EF Core, no matter what I do, I am unable to get the lambda in the Where statement to correctly evaluate to SQL. I have a basic generic LINQ query that takes an argument of type T where T :class, ICommonModel, this translates via a Repository that returns a IQueryable of TCommon where TCommon : :class, ICommonModel, but under the hood is using TData where TData : :class, IDataModel. I'm not sure exactly where the failure is, but no matter what it seems like it can't handle anything in the Where clause.

Here's the method with the Where clause.

public virtual async Task<IEnumerable<T>> GetAsync(IEnumerable<Guid> ids)
            => await this.repository.Read().Where(r => ids.Contains(r.Id)).ToListAsync();

Here's an even simpler statement that still fails, so it's not related to the Contains call as Initially thought.

public virtual async Task<IEnumerable<T>> GetAsync(IEnumerable<Guid> ids)
            => await this.repository.Read().Where(r => r.Id.ToString().Contains("0")).ToListAsync();

The error I get is

System.InvalidOperationException
  HResult=0x80131509
  Message=The LINQ expression 'DbSet<Person>
    .Where(p => new Person{ 
        ContactNumber = p.ContactNumber, 
        Email = p.Email, 
        FullName = p.FullName, 
        Id = p.Id 
    }
    .Id.ToString().Contains("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.
  Source=Microsoft.EntityFrameworkCore

Here's the Repo method

        public virtual IQueryable<TCommon> Read()
            => this.Context.Set<TData>().ProjectTo<TCommon>(this.Mapper.ConfigurationProvider);

The interface for both methods are identical.

    public interface IDataModel {

        Guid Id { get; set; }
    }
...
    public interface ICommonModel {
        Guid Id { get; set; }
    }
c#
entity-framework-core
automapper
.net-core-3.0
asked on Stack Overflow Jan 22, 2020 by TJ L • edited Jan 23, 2020 by TJ L

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0