You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
EF Core 7: The LINQ expression 'DbSet could not be translated when using Value Object #518
EF COre The LINQ expression 'DbSet 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
I have the following Name ValueObject:
public class Name : ValueObject
{
public string Value { get; private set; }
protected Name()
{
}
private Name(string value)
{
Value = value;
}
public static Result<Name> Create(string name)
{
name = (name ?? string.Empty).Trim();
if (string.IsNullOrEmpty(name))
return Result.Failure<Name>("Name should not be empty");
if (name.Length > 128)
return Result.Failure<Name>("Name is too long");
return Result.Success(new Name(name));
}
protected override IEnumerable<IComparable> GetEqualityComponents()
{
yield return Value;
}
}
and the following table
public class Team
{
public int Id { get; private set; }
public Name Name { get; private set; }
}
var teamQuery = await (from team in dbContext.Team.AsNoTracking()
where team.Name.Value.Contains(input.SearchText)
select new
{
Id = team.Id,
Name = team.Name.Value,
}).ToListAsync();
I get the following error:
- The LINQ expression 'DbSet<Team>()
.Where(t => t.Name.Value.Contains(__input_SearchText_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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync
It is working with the ComplexProperty mapping feature from EF Core 8.
varhostBuilder=Host.CreateEmptyApplicationBuilder(new(){Args=args});hostBuilder.Logging.AddConsole().SetMinimumLevel(LogLevel.Debug);hostBuilder.Services.AddDbContext<SampleDbContext>(b =>b.UseNpgsql(connectionString);varhost=hostBuilder.Build();varsampleDbContext=host.Services.GetRequiredService<SampleDbContext>();varteams=sampleDbContext.Set<Team>();teams.AddRange(newTeam{Name=new("some1")},newTeam{Name=new("anothersome")},newTeam{Name=new("newone")});sampleDbContext.SaveChanges();varteamDtos=awaitteams.AsNoTracking().Where(x =>x.Name.Value.Contains("some")).Select(x =>new{x.Id,Name=x.Name.ToString()}).ToListAsync();host.Services.GetRequiredService<ILogger<DbContext>>().LogDebug("Fetched teams: {@Teams}",teamDtos);publicclassSampleDbContext(DbContextOptionsoptions):DbContext(options){protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){}protectedoverridevoidOnModelCreating(ModelBuilderb){b.UseIdentityByDefaultColumns();b.Entity<Team>(static e =>{e.ToTable("teams");e.HasKey(x =>x.Id).HasName("teams_pk");e.Property(x =>x.Id).HasColumnName("id");// This one varname=e.ComplexProperty(x =>x.Name).IsRequired();name.Property(x =>x.Value).HasColumnName("name").IsRequired();});}}publicclassTeam{publiclongId{get;init;}publicrequiredNameName{get;init;}}publicclassName(stringvalue):SimpleValueObject<string>(value);
Activity
vkhorikov commentedon Sep 27, 2023
It most likely doesn't like the
Name.Valuepart.LINQ queries don't work well, both in EF and NH, so you'd need to come up with some workaround, unfortunately.
PNZeml commentedon Nov 24, 2023
It is working with the
ComplexPropertymapping feature from EF Core 8.