EntityFramework.DynamicFilters
EntityFramework.DynamicFilters copied to clipboard
Filtering on bi-directional 1:0-1 relationships
I've just checked out the source code and I can see there is a known issue (and unit test) for supporting relationships without having the foreign key specified on the model.
I am wondering if this the same reason why bi-directional 1:0-1 relationships don't work, or if this is due to another issue? Here is an example of a broken model based off the existing unit tests:
public class EntityE : EntityBase
{
public int ChildID { get; set; }
public EntityEChild Child { get; set; }
}
public class EntityEChild : EntityBase
{
public EntityE Parent { get; set; } // Link-back to my parent
public int ChildValue { get; set; }
}
modelBuilder.Entity<EntityE>().HasOptional(x => x.Child).WithOptionalPrincipal(x => x.Parent);
Could I modify DynamicFilterQueryVisitorCSpace to skip over the dependent side of the navigation property? There would be no need to follow Parent backwards as filtering should have already been applied prior to reaching EntityEChild in this instance right?
Any guidance/ideas on how I can (temporarily) resolve this issue would be grateful.
I've narrowed this down further. It works if the relationship is required on both sides, or a mixture of optional/required:
modelBuilder.Entity<EntityE>().HasRequired(x => x.Child).WithRequiredPrincipal(x => x.Parent);
modelBuilder.Entity<EntityE>().HasOptional(x => x.Child).WithRequired(x => x.Parent);
But it does not work if the relationship is optional on both sides
modelBuilder.Entity<EntityE>().HasOptional(x => x.Child).WithOptionalPrincipal(x => x.Parent);
Hello @rhyskoedijk ,
Thank you for reporting,
We will try to look at it in the beginning of next week.
Best Regards,
Jonathan
Hello @rhyskoedijk ,
Sorry for my late answer,
I have investigated this issue again today but it looks, for now, it's too much complex to solve as the constraint is null, unlike the second scenario that is working.
I believe, for now, we will abandon it but we may be trying to support it again in a few months.
Best Regards,
Jonathan
Understandable, we've worked around it for now by adjusting our domain model to not use optional 1:0-1 relationships. Not ideal, but gets us over the line for now.
Thanks for looking in to it