-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
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
EF Core 7: The LINQ expression 'DbSet could not be translated when using Value Object #518
Comments
It most likely doesn't like the LINQ queries don't work well, both in EF and NH, so you'd need to come up with some workaround, unfortunately. |
It is working with the var hostBuilder = Host.CreateEmptyApplicationBuilder(new() { Args = args });
hostBuilder.Logging.AddConsole().SetMinimumLevel(LogLevel.Debug);
hostBuilder.Services.AddDbContext<SampleDbContext>(b => b.UseNpgsql(connectionString);
var host = hostBuilder.Build();
var sampleDbContext = host.Services.GetRequiredService<SampleDbContext>();
var teams = sampleDbContext.Set<Team>();
teams.AddRange(new Team {Name = new("some1")}, new Team {Name = new("anothersome")}, new Team {Name = new("newone")});
sampleDbContext.SaveChanges();
var teamDtos = await teams.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);
public class SampleDbContext(DbContextOptions options) : DbContext(options) {
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { }
protected override void OnModelCreating(ModelBuilder b) {
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
var name = e.ComplexProperty(x => x.Name).IsRequired();
name.Property(x => x.Value).HasColumnName("name").IsRequired();
});
}
}
public class Team {
public long Id { get; init; }
public required Name Name { get; init; }
}
public class Name(string value) : SimpleValueObject<string>(value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
and the following table
and in my dbcontext
When I try to run the following
I get the following error:
The text was updated successfully, but these errors were encountered: