-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Using EF.Property to target included entity property #17099
Comments
EF.Property can be used to access property or navigation defined in EF Core metadata. But the function does not parse multi-level as you intend. The proper way to write such query would be like this. var fail = await Dependencies.EntityServices.FavoriteExerciseService.GetAll()
.Include(m => m.Exercise)
.Where(m => EF.Functions.Contains(EF.Property<string>(EF.Property<Exercise>(m, "Exercise"), "DisplayName"), "\"*press*\""))
.ToListAsync(); The 2nd fail query is not valid since the property or navigation name is not passed as string. Compilation would fail. |
Also you don't need include to access property on a navigation in where predicate. You would only need include if you are projecting the entity and want the navigation to be populated for the materialized entity. |
@smitpatel thank you! That indeed works. Though, the problem I have is that Edit: Just tried using interface var ok = await Dependencies.EntityServices.FavoriteExerciseService.GetAll()
.Include(m => m.Exercise)
.Where(m => EF.Functions.Contains(EF.Property<string>(EF.Property<IEntity>(m, "Exercise"), "DisplayName"), "\"*press*\""))
.ToListAsync(); Is this a valid way of using the |
The generic constraint on EF.Property is to allow you to compose over while writing query. (e.g. doing |
@smitpatel it seems that this has stopped working in the This works ok: await Dependencies.EntityServices.FavoriteExerciseService.GetAll()
.Where(m => EF.Functions.Contains(
EF.Property<string>(EF.Property<Exercise>(m, "Exercise"), "DisplayName"), "\"*press*\""))
.ToListAsync(); This does not: await Dependencies.EntityServices.FavoriteExerciseService.GetAll()
.Where(m => EF.Functions.Contains(
EF.Property<string>(EF.Property<IEntity>(m, "Exercise"), "DisplayName"), "\"*press*\""))
.ToListAsync(); The exception thrown is:
Is this behavior intentional? Or maybe there is indeed a way of providing the |
Likely, somewhere a convert to interface node got added which caused client eval. Currently it is not possible for us to bind EF Property to an interface type as we don't have metadata about interface like we have for EntityType. |
That seems strange as it just worked :/ Can you think of any workaround? Other than possibly writing the SQL manually as that is not that simple to put it in our code as we are working with IQueryable and have no access DbSet (where sql methods were moved) |
Easy work-around, don't use interfaces. If you use concrete type (which is mapped in EF Core model), it will work. |
I do have a |
I was looking at first post to see query, can you use |
Yeah, but the query is just an example. The .Where(m => EF.Functions.Contains(EF.Property<string>(EF.Property<IEntity>(m, "Exercise"), "DisplayName"), "\"*press*\"")) Is applied dynamically on potentially any DbSet with any property and any navigation property. Imho, it would be awesome to be able to pass type to the |
Triage: probably need to punt this too. More specifically, the value of this is low without a means to work with non-generic |
Hi!
Is it possible to use
EF.Property<>
to target property of included entities? Following attempts:throw
The
DisplayName
onExercise
type is properly indexed and this works and returns correct results:I also tried running following query on SQL which also returns proper results.
Thank you for any help or suggestions.
Further technical details
EF Core version: 3.0.0-preview5.19227.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 10
IDE: Visual Studio 2019 preview
The text was updated successfully, but these errors were encountered: