-
-
Notifications
You must be signed in to change notification settings - Fork 158
Cannot sort resource having a relationship with EagerLoadAttribute #988
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
Comments
Hi @ThomasBarnekow, thanks for your elaborate description. I've never tried combining an exposed relationship with I haven't debugged why exactly sorting breaks, but I advise to remove the Example: public sealed class EngagementResourceDefinition : JsonApiResourceDefinition<Engagement, Guid>
{
public EngagementResourceDefinition(IResourceGraph resourceGraph)
: base(resourceGraph)
{
}
public override IReadOnlyCollection<IncludeElementExpression> OnApplyIncludes(IReadOnlyCollection<IncludeElementExpression> existingIncludes)
{
ResourceContext engagementContext = ResourceGraph.GetResourceContext<Engagement>();
RelationshipAttribute partiesRelationship = engagementContext.Relationships.Single(relationship => relationship.Property.Name == nameof(Engagement.Parties));
HashSet<IncludeElementExpression> newIncludes = existingIncludes.ToHashSet();
newIncludes.Add(new IncludeElementExpression(partiesRelationship));
return newIncludes;
}
} |
Hi @bart-degreed, thanks for coming back so quickly. I've tried your suggestion, testing it in the browser, but it does not work quite as expected (although I don't know what exactly to expect). When visiting
You'd have to use
What form of output would you expect in this case? Anyhow, other than with
With The difference made by the So, unfortunately, this has not yet solved my issue. Would you have expected a different behavior? Am I trying something that should not be done? At the moment, what I would do is to simply filter the resources by using |
You're right, my suggestion does not work. It should return an Let's rewind a bit to ensure we're looking at the same. I've created a PR to repro your original scenario (without I'm not saying that the combination of |
While I am always using explicit foreign keys such as modelBuilder.Entity<Engagement>(engagement =>
{
engagement.HasKey(t => t.Id);
engagement.Property(t => t.Id).ValueGeneratedOnAdd();
});
modelBuilder.Entity<EngagementParty>(party =>
{
party.HasKey(e => e.Id);
party.Property(e => e.Id).ValueGeneratedOnAdd();
party.HasOne(ep => ep.Engagement)
.WithMany(e => e.Parties)
.HasForeignKey(ep => ep.EngagementId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
}); I would have said "I don't get that error", but I now remember that I did in fact get it, too, until I did what was indicated by the error message. I made EF core ignore that issue because it is none. Here's how I configured my DB Context based on the hint I got: services.AddDbContext<MyDbContext>(builder =>
{
builder.UseSqlServer(Configuration["ConnectionStrings:SqlServerConnection"]);
builder.ConfigureWarnings(wcb => wcb.Ignore(CoreEventId.InvalidIncludePathError));
}); |
For simplicity I try not to write unneeded code. Except for |
In my data model, I needed enough of those definitions so that I decided I'd rather produce a complete and explicit specification of the model than a partial one that relied on some EF Core magic while specifying some aspects explicitly. The portion that is not strictly necessary is small enough so that the benefit of having everything explicit and in one place outweighs the disadvantage of writing "unneded code". |
DESCRIPTION
Before describing my issue, let me first thank you for this project. It is absolutely awesome.
On to my issue. I have a resource called
Engagement
with severalHasMany
relationships. Three of those are related in the following way: The first one,Parties
, is the navigation property in an EF core relationship betweenEngagement
andEngagementParty
(where one engagement can have many engagement parties). The other two,FirstParties
andSecondParties
, areNotMapped
from an EF Core perspective and derived fromParties
by selecting a subset of the elements contained inParties
. Based on your documentation, I've added theEagerLoad
attribute to theParties
relationship. This also works, meaning that I can GET theFirstParties
andSecondParties
. However, what does not work is sorting theEngagementParty
resources by any of their attributes (e.g.,ShortName
). For example:produces the same order as
which produces the same order as
When I remove the
EagerLoad
attribute from theParties
relationship, the resources can be sorted as expected.The same restriction/issue applies to sort orders applied by resource definitions (in the
OnApplySort()
method). WithEagerLoad
, the sort order has no effect. WithoutEagerLoad
the sort order is applied correctly.STEPS TO REPRODUCE
Here are the
Engagement
andEngagementParty
entities. Note theEagerLoad
attribute on theParties
property.Here is the resource definition that establishes a default sort order:
It is registered in the
Startup
class as follows:With the
EagerLoad
attribute, sorting does not work. When removing theEagerLoad
attribute, sorting works as expected.EXPECTED BEHAVIOR
Sorting works with the
EagerLoad
attribute applied to theParties
relationship property.ACTUAL BEHAVIOR
Sorting does not work with the
EagerLoad
attribute applied to theParties
relationship property.VERSIONS USED
4.1.1
5.0
5.0
MSSQLLocalDB
(SQL Server 13.0.4001
)The text was updated successfully, but these errors were encountered: