How can I clean up my entity 'includes' to reuse the same set of statements? I'm trying to reuse a set of Includes while staying DRY.
Before
_context.Accounts
.Include(x=> x.Status)
.Include(x=> x.Type)
.Include(x=> x.Phones)
.Include(x=> x.Users)
.Include(x=> x.Admins)
After:
_context.Accounts.CustomIncludes()
Try this:
public static class DataExtensions
{
public static Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<Account, List<Admin>> CustomIncludes(this DbSet<Account> accounts)
{
return accounts
.Include(p => p.Status)
.Include(p => p.Type)
.Include(p => p.Phones)
.Include(p => p.Users)
.Include(p => p.Admins);
}
}
Then you can say
context.Accounts.CustomIncludes();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With