EF DbContext on a generic repository does not perform the expected includes when transforming the data into a...
This question already has an answer here:
EF Generic Repository Multiple Includes
1 answer
I have a generic/base repository that I'm using and it contains different variety of methods for accessing my database.
Here's a sample:
public IQueryable<T> FindAll(string includedProperties = null)
{
var setContext = _context.Set<T>();
if (includedProperties != null)
{
foreach (var property in includedProperties)
{
setContext.Include(property);
}
}
return setContext;
}
I'm calling this method by:
var employees = _employeeRepository.FindAll(new string { "JobPosition", "Department", "EmployeeTeams", "EmployeeTeams.Team" })
When I perform a ToList() on employees, the expected navigation property is not included.
I tried switching it up and did the following:
var employees = _employeeRepository.FindAll().Include("JobPosition")
.Include("Department")
.Include("EmployeeTeams")
.Include("EmployeeTeams.Team")
And performing the ToList() includes the specified navigation property.
I want to make my approach on my generic repository work. Any ideas on how can I fix this?
NOTE: I also tried including navigation properties via expressions
(Expression<Func<T, object>> predicate)
still the same result.
linq .net-core entity-framework-core
marked as duplicate by Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 7:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
EF Generic Repository Multiple Includes
1 answer
I have a generic/base repository that I'm using and it contains different variety of methods for accessing my database.
Here's a sample:
public IQueryable<T> FindAll(string includedProperties = null)
{
var setContext = _context.Set<T>();
if (includedProperties != null)
{
foreach (var property in includedProperties)
{
setContext.Include(property);
}
}
return setContext;
}
I'm calling this method by:
var employees = _employeeRepository.FindAll(new string { "JobPosition", "Department", "EmployeeTeams", "EmployeeTeams.Team" })
When I perform a ToList() on employees, the expected navigation property is not included.
I tried switching it up and did the following:
var employees = _employeeRepository.FindAll().Include("JobPosition")
.Include("Department")
.Include("EmployeeTeams")
.Include("EmployeeTeams.Team")
And performing the ToList() includes the specified navigation property.
I want to make my approach on my generic repository work. Any ideas on how can I fix this?
NOTE: I also tried including navigation properties via expressions
(Expression<Func<T, object>> predicate)
still the same result.
linq .net-core entity-framework-core
marked as duplicate by Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 7:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
EF Generic Repository Multiple Includes
1 answer
I have a generic/base repository that I'm using and it contains different variety of methods for accessing my database.
Here's a sample:
public IQueryable<T> FindAll(string includedProperties = null)
{
var setContext = _context.Set<T>();
if (includedProperties != null)
{
foreach (var property in includedProperties)
{
setContext.Include(property);
}
}
return setContext;
}
I'm calling this method by:
var employees = _employeeRepository.FindAll(new string { "JobPosition", "Department", "EmployeeTeams", "EmployeeTeams.Team" })
When I perform a ToList() on employees, the expected navigation property is not included.
I tried switching it up and did the following:
var employees = _employeeRepository.FindAll().Include("JobPosition")
.Include("Department")
.Include("EmployeeTeams")
.Include("EmployeeTeams.Team")
And performing the ToList() includes the specified navigation property.
I want to make my approach on my generic repository work. Any ideas on how can I fix this?
NOTE: I also tried including navigation properties via expressions
(Expression<Func<T, object>> predicate)
still the same result.
linq .net-core entity-framework-core
This question already has an answer here:
EF Generic Repository Multiple Includes
1 answer
I have a generic/base repository that I'm using and it contains different variety of methods for accessing my database.
Here's a sample:
public IQueryable<T> FindAll(string includedProperties = null)
{
var setContext = _context.Set<T>();
if (includedProperties != null)
{
foreach (var property in includedProperties)
{
setContext.Include(property);
}
}
return setContext;
}
I'm calling this method by:
var employees = _employeeRepository.FindAll(new string { "JobPosition", "Department", "EmployeeTeams", "EmployeeTeams.Team" })
When I perform a ToList() on employees, the expected navigation property is not included.
I tried switching it up and did the following:
var employees = _employeeRepository.FindAll().Include("JobPosition")
.Include("Department")
.Include("EmployeeTeams")
.Include("EmployeeTeams.Team")
And performing the ToList() includes the specified navigation property.
I want to make my approach on my generic repository work. Any ideas on how can I fix this?
NOTE: I also tried including navigation properties via expressions
(Expression<Func<T, object>> predicate)
still the same result.
This question already has an answer here:
EF Generic Repository Multiple Includes
1 answer
linq .net-core entity-framework-core
linq .net-core entity-framework-core
asked Nov 23 '18 at 6:37
Luke VillanuevaLuke Villanueva
1,02452778
1,02452778
marked as duplicate by Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 7:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 7:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Include returns IQueryable (or IIncludableQueryable depending how you call it) so you need to add them after each other.
Try it like this
foreach (var property in includedProperties)
{
setContext = setContext.Include(property);
}
1
I was actually trying to do this but it didn't crossed my mind that I can set the DbSet<T> to queryable. Thank you for this.
– Luke Villanueva
Nov 23 '18 at 7:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Include returns IQueryable (or IIncludableQueryable depending how you call it) so you need to add them after each other.
Try it like this
foreach (var property in includedProperties)
{
setContext = setContext.Include(property);
}
1
I was actually trying to do this but it didn't crossed my mind that I can set the DbSet<T> to queryable. Thank you for this.
– Luke Villanueva
Nov 23 '18 at 7:59
add a comment |
Include returns IQueryable (or IIncludableQueryable depending how you call it) so you need to add them after each other.
Try it like this
foreach (var property in includedProperties)
{
setContext = setContext.Include(property);
}
1
I was actually trying to do this but it didn't crossed my mind that I can set the DbSet<T> to queryable. Thank you for this.
– Luke Villanueva
Nov 23 '18 at 7:59
add a comment |
Include returns IQueryable (or IIncludableQueryable depending how you call it) so you need to add them after each other.
Try it like this
foreach (var property in includedProperties)
{
setContext = setContext.Include(property);
}
Include returns IQueryable (or IIncludableQueryable depending how you call it) so you need to add them after each other.
Try it like this
foreach (var property in includedProperties)
{
setContext = setContext.Include(property);
}
answered Nov 23 '18 at 7:39
Marcus HöglundMarcus Höglund
9,90062748
9,90062748
1
I was actually trying to do this but it didn't crossed my mind that I can set the DbSet<T> to queryable. Thank you for this.
– Luke Villanueva
Nov 23 '18 at 7:59
add a comment |
1
I was actually trying to do this but it didn't crossed my mind that I can set the DbSet<T> to queryable. Thank you for this.
– Luke Villanueva
Nov 23 '18 at 7:59
1
1
I was actually trying to do this but it didn't crossed my mind that I can set the DbSet<T> to queryable. Thank you for this.
– Luke Villanueva
Nov 23 '18 at 7:59
I was actually trying to do this but it didn't crossed my mind that I can set the DbSet<T> to queryable. Thank you for this.
– Luke Villanueva
Nov 23 '18 at 7:59
add a comment |