EfCoreExtensions.FindParent
2.0.0
dotnet add package EfCoreExtensions.FindParent --version 2.0.0
NuGet\Install-Package EfCoreExtensions.FindParent -Version 2.0.0
<PackageReference Include="EfCoreExtensions.FindParent" Version="2.0.0" />
paket add EfCoreExtensions.FindParent --version 2.0.0
#r "nuget: EfCoreExtensions.FindParent, 2.0.0"
// Install EfCoreExtensions.FindParent as a Cake Addin #addin nuget:?package=EfCoreExtensions.FindParent&version=2.0.0 // Install EfCoreExtensions.FindParent as a Cake Tool #tool nuget:?package=EfCoreExtensions.FindParent&version=2.0.0
FindParent
public static IQueryable<TParent> FindParent<TChild, TParent>(this IQueryable<TChild> queryable)
An entity framework core linq extension method to find a parent entity any number of ancestors away. Traverses the db context model to find the optimal path, then dynamically generates a queryable of chained selects along the path. Example:
Say there is a table structure where Garages have multiple Cars, Cars have multiple Tires, and Tires have multiple Bolts. If you wanted to know the parent Garage of the Bolt with an Id of 1, you could get it via:
Garage garageFromBolt = context.Bolts.Where(b => b.Id == 1).FindParent<Bolt, Garage>().FirstOrDefault();
The method uses Dijkstra’s shortest path algorithm to find the shortest path to the parent. (Meaning the translated SQL has the lowest number of expensive joins!)
Since it needs access to the context model, you'll need to initially provide that like so:
using static FindParent.Extensions;
SetContextModel(yourDbContext.Model);
A convenient place to do this might be OnModelCreating
.
Possible Exceptions
- If the child table has a composite key, will throw a NotSupportedException.
- If either the child or parent Types do not correspond to tables in the context model, will throw an ArgumentException.
- If the child id's type is not assignable to the child table's primary key property, will throw an ArgumentException.
Source: https://github.com/CalebBertrand/EfCoreExtensions.FindParent
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
New API extending linq instead of DbSet.