EF DbContext on a generic repository does not perform the expected includes when transforming the data into a...












0
















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.










share|improve this question













marked as duplicate by Ivan Stoev entity-framework-core
Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

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.























    0
















    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.










    share|improve this question













    marked as duplicate by Ivan Stoev entity-framework-core
    Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

    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.





















      0












      0








      0









      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.










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 6:37









      Luke VillanuevaLuke Villanueva

      1,04452879




      1,04452879




      marked as duplicate by Ivan Stoev entity-framework-core
      Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

      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 entity-framework-core
      Users with the  entity-framework-core badge can single-handedly close entity-framework-core questions as duplicates and reopen them as needed.

      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.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          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);
          }





          share|improve this answer



















          • 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 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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);
          }





          share|improve this answer



















          • 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
















          0














          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);
          }





          share|improve this answer



















          • 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














          0












          0








          0







          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);
          }





          share|improve this answer













          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);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 7:39









          Marcus HöglundMarcus Höglund

          10.1k62750




          10.1k62750








          • 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





            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





          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()