Comments (17)
I know this has been closed, I just wanted to add you can get around the refactoring issue with magical strings by doing something like this:
$"{nameof(PriceList.Revisions)}.{nameof(PriceListRevision.VehicleClassPrices)}.{nameof(VehicleClassPricing.Product)}",
That's what I do. Anyway, even so, an expression based solution would be nicer, ofc.
from eshoponweb.
I like the nameof
solution. I've just added it via the PR noted above.
from eshoponweb.
For now this is implemented using string-based includes, to maximize flexibility. The downside is, they're magic strings and could become out of sync with entity renames.
Example usage:
public CustomerOrdersWithItemsSpecification(string buyerId)
{
_buyerId = buyerId;
AddInclude(o => o.OrderItems); // lambda syntax works for immediate children
AddInclude("OrderItems.ItemOrdered"); // string syntax works for any level of depth
}
from eshoponweb.
In case someone still wants to have this feature. I added a pull request that adds the possibility to chain includes with .Include() and .ThenInclude().
c3193b1
from eshoponweb.
I was wondering the same the thing. I had to change the specifications to use strings instead of expressions to do nested includes. Not entirely ideal because refactoring can easily break the includes.
Sent from my Google Nexus 6P using FastHub
from eshoponweb.
I'm not sure if there's a supported way to do this (hence why it's not implemented yet). I'm open to suggestions, though. This is somewhat related (TL;DR: you only need ThenInclude for collection navigation properties):
dotnet/efcore#4716
from eshoponweb.
Related: dotnet/efcore#9523
from eshoponweb.
@ModernRonin I cannot quite follow with your example, can you use @ardalis 's example with
OrderItems.ItemOrdered
and give an example using nameof() Thanks
from eshoponweb.
@ardalis have you find any other alternative approach to avoid using"magic string" with Specification pattern. Thanks
from eshoponweb.
Thanks.
from eshoponweb.
For some reason this multiple nesting is not working for me. Can anyone please assist me on where I'm wrong?
Here are my Classes:
Branch:
public class Branch : BaseEntity<Guid>, IAggregateRoot
{
private Branch()
{
// required by EF
}
public Guid ClientId { get; set; }
public Client Client { get; set; }
}
Client:
public class Client : BaseEntity<Guid>, IAggregateRoot
{
private Client()
{
// required by EF
}
public Guid BusinessId { get; set; }
public Business Business { get; set; }
}
Business:
public class Business : BaseEntity<Guid>, IAggregateRoot
{
private Business()
{
// required by EF
}
public string Name { get; set; }
}
I'm trying to load Branch
with Client
and Business
. Here is my specification:
public class BranchWithClientSpecification : BaseSpecification<Branch>
{
public BranchWithClientSpecification() : base(b => b.Active)
{
AddInclude(b => b.Client);
AddInclude($"{nameof(Branch.Client)}.{nameof(Client.Business)}");
}
}
AddInclude($"{nameof(Branch.Client)}.{nameof(Client.Business)}");
line is not working. I always get null for public Business Business { get; set; }
property in Client
class. Can anyone please assist on where I'm wrong?
from eshoponweb.
I think that @mrukas is an interesting solution.
We are trying to use Include & .ThenInclude but also use filtering includes. With strings magic that is not possible.
ref: https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager#filtered-include
from eshoponweb.
I think that @mrukas is an interesting solution.
We are trying to use Include & .ThenInclude but also use filtering includes. With strings magic that is not possible.
ref: https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager#filtered-include
Actually, I found the Ardalis.Specification package, I think that is the solution, but if we want to build a simpler solution, is not as easy as I expected
from eshoponweb.
Yes, this is solved already in Ardalis.Specification, if you're able to just consume that package.
from eshoponweb.
We have a simplistic solution, but we need to use include and then-include with filtering. It is not easy to add without break all
from eshoponweb.
If you use the latest version of Ardalis.Specification
, you'll get chaining capabilities for Include-TheInclude.
The filtered includes, e.g. Include(x => x.Where(something))
, are introduced starting with EF Core 5
. So you have to use a minimum of that version of EF Core to get that capability.
from eshoponweb.
@fiseni Yes, we are using EF core 5
.
Regarding using Ardalis.Specification
, We have just implemented an Specification, so move to another implementation is not a good idea. (Next project will be mandatory use that package)
Anyway, I have implemented some pieces similar to Ardalis.Specification
solution, but more simplistic (not as good as Ardalis, of course)
We have an issue with Nested ThenIncludes and IncludeExtension.cs, instead of returing IQueryable
, our code returns IIncludableQueryable
(for chaining of chaining), also we have changed the generic implementation of that method:
public static IIncludableQueryable<T, TProperty> Include<T, TProperty>(this IQueryable<T> source, IncludeExpressionInfo info)
{
_ = info ?? throw new ArgumentNullException(nameof(info));
var types = new Type[] {
info.EntityType,
info.PropertyType
};
var methodInfo = typeof(EntityFrameworkQueryableExtensions).GetMethods().Where(m => m.Name.Equals("Include")).First();
var genericMethod = methodInfo.MakeGenericMethod(types);
var result = genericMethod.Invoke(null, new object[] { source, info.LambdaExpression });
return (IIncludableQueryable<T, TProperty>)result;
}
In the end, we have nested .ThenInclude
. Thanks all for building these projects.
(Nota: I don't know if Ardalis.Specification allows nested ThenIncludes[?])
from eshoponweb.
Related Issues (20)
- OBE
- Verify devcontainer for codespaces debugging HOT 6
- Modernize to .NET SDK Containers
- Inconsistent naming of async methods HOT 1
- How to connect to the postgres instance from the host ? HOT 1
- Shopify manager HOT 2
- public api get catalog brands throws error HOT 1
- How to debug this project using VS Code? HOT 1
- Architect Modern Web Applications edition 7th not available as PDF HOT 2
- incorrect about section in github description HOT 1
- Inject IHttpContextAccessor to CatalogContext's constructor will be destoryed when RevokeAuthenticationEvents.cs handle about CatalogContext's data
- The style class prefix "col-xs-*" didnt work in cshtml files. HOT 1
- Changelog for the latest version, updated to .NET 7, is not available in the wiki page HOT 4
- Update to bootstrap 5
- .NET 8 Updates HOT 5
- Facing issue when deploying the application on azure app service HOT 2
- I am not able to deploy the application to Azure from Visual Studio Code HOT 1
- I'm not able to update in memory database HOT 1
- Update NuGet packages to latest versions HOT 1
- Revalidate AZD HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eshoponweb.