How to properly configure lazy loading












0















In the view model below, I can see that part.CountryCode is always null.



I can get the data if I use db.Parts.Include(p => p.CountryCode), but I would like to set up lazy loading for now. Does anyone know what I'm missing?



I'm using:





  • Microsoft.EntityFrameworkCore (2.1.4) in a NETStandard.Library(2.0.3)

  • UI is WPF with .NET 4.6.1


ViewModel



class PagePartsViewModel : BaseViewModel
{
public ObservableCollection<PartViewModel> Parts { get; set; }

public PagePartsViewModel()
{
Parts = new ObservableCollection<PartViewModel>();
var parts = StandardDatabase.Commands.GetParts();
foreach (var part in parts)
{
Parts.Add(new PartViewModel(part));
}
}
}


GetParts()



public static List<Part> GetParts()
{
using (var db = new ApplicationDbContext())
{
return db.Parts.ToList();
}
}


ApplicationDbContext.OnConfiguring()



optionsBuilder
.UseLazyLoadingProxies()
.UseSqlServer("Connection String");


ApplicationDbContext.OnModelCreating()



builder.Entity<Part>()
.HasOne(p => p.CountryCode);
builder.Entity<CountryCode>()
.HasAlternateKey(cc => cc.Code);


Part.cs



public class Part : EntityBase
{
public string OEMNumber { get; set; }
public string Description { get; set; }
public string CountryOfOrigin { get; set; }
[Column(TypeName = "decimal(10, 2)")]
public decimal BellUnitCost { get; set; }
[Column(TypeName = "decimal(9, 3)")]
public double Weight { get; set; }

public virtual StockQuantity StockQuantity { get; set; }
public virtual ICollection<PartQuantity> PartQuantities { get; set; }

public virtual CountryCode CountryCode { get; set; }
}


CountryCode.cs



public class CountryCode : EntityBase
{
public string Code { get; set; }
public string FriendlyName { get; set; }

public virtual ICollection<Part> Parts { get; set; }
}









share|improve this question



























    0















    In the view model below, I can see that part.CountryCode is always null.



    I can get the data if I use db.Parts.Include(p => p.CountryCode), but I would like to set up lazy loading for now. Does anyone know what I'm missing?



    I'm using:





    • Microsoft.EntityFrameworkCore (2.1.4) in a NETStandard.Library(2.0.3)

    • UI is WPF with .NET 4.6.1


    ViewModel



    class PagePartsViewModel : BaseViewModel
    {
    public ObservableCollection<PartViewModel> Parts { get; set; }

    public PagePartsViewModel()
    {
    Parts = new ObservableCollection<PartViewModel>();
    var parts = StandardDatabase.Commands.GetParts();
    foreach (var part in parts)
    {
    Parts.Add(new PartViewModel(part));
    }
    }
    }


    GetParts()



    public static List<Part> GetParts()
    {
    using (var db = new ApplicationDbContext())
    {
    return db.Parts.ToList();
    }
    }


    ApplicationDbContext.OnConfiguring()



    optionsBuilder
    .UseLazyLoadingProxies()
    .UseSqlServer("Connection String");


    ApplicationDbContext.OnModelCreating()



    builder.Entity<Part>()
    .HasOne(p => p.CountryCode);
    builder.Entity<CountryCode>()
    .HasAlternateKey(cc => cc.Code);


    Part.cs



    public class Part : EntityBase
    {
    public string OEMNumber { get; set; }
    public string Description { get; set; }
    public string CountryOfOrigin { get; set; }
    [Column(TypeName = "decimal(10, 2)")]
    public decimal BellUnitCost { get; set; }
    [Column(TypeName = "decimal(9, 3)")]
    public double Weight { get; set; }

    public virtual StockQuantity StockQuantity { get; set; }
    public virtual ICollection<PartQuantity> PartQuantities { get; set; }

    public virtual CountryCode CountryCode { get; set; }
    }


    CountryCode.cs



    public class CountryCode : EntityBase
    {
    public string Code { get; set; }
    public string FriendlyName { get; set; }

    public virtual ICollection<Part> Parts { get; set; }
    }









    share|improve this question

























      0












      0








      0








      In the view model below, I can see that part.CountryCode is always null.



      I can get the data if I use db.Parts.Include(p => p.CountryCode), but I would like to set up lazy loading for now. Does anyone know what I'm missing?



      I'm using:





      • Microsoft.EntityFrameworkCore (2.1.4) in a NETStandard.Library(2.0.3)

      • UI is WPF with .NET 4.6.1


      ViewModel



      class PagePartsViewModel : BaseViewModel
      {
      public ObservableCollection<PartViewModel> Parts { get; set; }

      public PagePartsViewModel()
      {
      Parts = new ObservableCollection<PartViewModel>();
      var parts = StandardDatabase.Commands.GetParts();
      foreach (var part in parts)
      {
      Parts.Add(new PartViewModel(part));
      }
      }
      }


      GetParts()



      public static List<Part> GetParts()
      {
      using (var db = new ApplicationDbContext())
      {
      return db.Parts.ToList();
      }
      }


      ApplicationDbContext.OnConfiguring()



      optionsBuilder
      .UseLazyLoadingProxies()
      .UseSqlServer("Connection String");


      ApplicationDbContext.OnModelCreating()



      builder.Entity<Part>()
      .HasOne(p => p.CountryCode);
      builder.Entity<CountryCode>()
      .HasAlternateKey(cc => cc.Code);


      Part.cs



      public class Part : EntityBase
      {
      public string OEMNumber { get; set; }
      public string Description { get; set; }
      public string CountryOfOrigin { get; set; }
      [Column(TypeName = "decimal(10, 2)")]
      public decimal BellUnitCost { get; set; }
      [Column(TypeName = "decimal(9, 3)")]
      public double Weight { get; set; }

      public virtual StockQuantity StockQuantity { get; set; }
      public virtual ICollection<PartQuantity> PartQuantities { get; set; }

      public virtual CountryCode CountryCode { get; set; }
      }


      CountryCode.cs



      public class CountryCode : EntityBase
      {
      public string Code { get; set; }
      public string FriendlyName { get; set; }

      public virtual ICollection<Part> Parts { get; set; }
      }









      share|improve this question














      In the view model below, I can see that part.CountryCode is always null.



      I can get the data if I use db.Parts.Include(p => p.CountryCode), but I would like to set up lazy loading for now. Does anyone know what I'm missing?



      I'm using:





      • Microsoft.EntityFrameworkCore (2.1.4) in a NETStandard.Library(2.0.3)

      • UI is WPF with .NET 4.6.1


      ViewModel



      class PagePartsViewModel : BaseViewModel
      {
      public ObservableCollection<PartViewModel> Parts { get; set; }

      public PagePartsViewModel()
      {
      Parts = new ObservableCollection<PartViewModel>();
      var parts = StandardDatabase.Commands.GetParts();
      foreach (var part in parts)
      {
      Parts.Add(new PartViewModel(part));
      }
      }
      }


      GetParts()



      public static List<Part> GetParts()
      {
      using (var db = new ApplicationDbContext())
      {
      return db.Parts.ToList();
      }
      }


      ApplicationDbContext.OnConfiguring()



      optionsBuilder
      .UseLazyLoadingProxies()
      .UseSqlServer("Connection String");


      ApplicationDbContext.OnModelCreating()



      builder.Entity<Part>()
      .HasOne(p => p.CountryCode);
      builder.Entity<CountryCode>()
      .HasAlternateKey(cc => cc.Code);


      Part.cs



      public class Part : EntityBase
      {
      public string OEMNumber { get; set; }
      public string Description { get; set; }
      public string CountryOfOrigin { get; set; }
      [Column(TypeName = "decimal(10, 2)")]
      public decimal BellUnitCost { get; set; }
      [Column(TypeName = "decimal(9, 3)")]
      public double Weight { get; set; }

      public virtual StockQuantity StockQuantity { get; set; }
      public virtual ICollection<PartQuantity> PartQuantities { get; set; }

      public virtual CountryCode CountryCode { get; set; }
      }


      CountryCode.cs



      public class CountryCode : EntityBase
      {
      public string Code { get; set; }
      public string FriendlyName { get; set; }

      public virtual ICollection<Part> Parts { get; set; }
      }






      c# .net-core entity-framework-core lazy-loading .net-standard






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 14:07









      BassieBassie

      3,9322051




      3,9322051
























          1 Answer
          1






          active

          oldest

          votes


















          1














          using (var db = new ApplicationDbContext())
          {
          return db.Parts.ToList();
          }


          The using block disposes your dbContext once it finishes loading your parts. Lazy-loading requires the dbContext to still be alive.






          share|improve this answer
























          • Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend that Commands implements IDisposable so that I can wrap GetParts() in a using Commands()?

            – Bassie
            Nov 24 '18 at 14:30











          • Personally I would recommend just including any navigation properties you need ahead of time. I suggest using dependency injection and having your class accept an instance of dbContext and then scoping the lifetime of the dbContext to the lifetime of the request (I'm not sure how that works with WPF, but the dbContext lifetime shouldn't be the responsibility of your NetStandard library)

            – Brad M
            Nov 24 '18 at 14:36











          • I would like to try that but I'm not sure how I can inject a .net-core DbContext into a .net-framework app

            – Bassie
            Nov 24 '18 at 14:42











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458982%2fhow-to-properly-configure-lazy-loading%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          using (var db = new ApplicationDbContext())
          {
          return db.Parts.ToList();
          }


          The using block disposes your dbContext once it finishes loading your parts. Lazy-loading requires the dbContext to still be alive.






          share|improve this answer
























          • Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend that Commands implements IDisposable so that I can wrap GetParts() in a using Commands()?

            – Bassie
            Nov 24 '18 at 14:30











          • Personally I would recommend just including any navigation properties you need ahead of time. I suggest using dependency injection and having your class accept an instance of dbContext and then scoping the lifetime of the dbContext to the lifetime of the request (I'm not sure how that works with WPF, but the dbContext lifetime shouldn't be the responsibility of your NetStandard library)

            – Brad M
            Nov 24 '18 at 14:36











          • I would like to try that but I'm not sure how I can inject a .net-core DbContext into a .net-framework app

            – Bassie
            Nov 24 '18 at 14:42
















          1














          using (var db = new ApplicationDbContext())
          {
          return db.Parts.ToList();
          }


          The using block disposes your dbContext once it finishes loading your parts. Lazy-loading requires the dbContext to still be alive.






          share|improve this answer
























          • Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend that Commands implements IDisposable so that I can wrap GetParts() in a using Commands()?

            – Bassie
            Nov 24 '18 at 14:30











          • Personally I would recommend just including any navigation properties you need ahead of time. I suggest using dependency injection and having your class accept an instance of dbContext and then scoping the lifetime of the dbContext to the lifetime of the request (I'm not sure how that works with WPF, but the dbContext lifetime shouldn't be the responsibility of your NetStandard library)

            – Brad M
            Nov 24 '18 at 14:36











          • I would like to try that but I'm not sure how I can inject a .net-core DbContext into a .net-framework app

            – Bassie
            Nov 24 '18 at 14:42














          1












          1








          1







          using (var db = new ApplicationDbContext())
          {
          return db.Parts.ToList();
          }


          The using block disposes your dbContext once it finishes loading your parts. Lazy-loading requires the dbContext to still be alive.






          share|improve this answer













          using (var db = new ApplicationDbContext())
          {
          return db.Parts.ToList();
          }


          The using block disposes your dbContext once it finishes loading your parts. Lazy-loading requires the dbContext to still be alive.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 14:25









          Brad MBrad M

          7,11111633




          7,11111633













          • Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend that Commands implements IDisposable so that I can wrap GetParts() in a using Commands()?

            – Bassie
            Nov 24 '18 at 14:30











          • Personally I would recommend just including any navigation properties you need ahead of time. I suggest using dependency injection and having your class accept an instance of dbContext and then scoping the lifetime of the dbContext to the lifetime of the request (I'm not sure how that works with WPF, but the dbContext lifetime shouldn't be the responsibility of your NetStandard library)

            – Brad M
            Nov 24 '18 at 14:36











          • I would like to try that but I'm not sure how I can inject a .net-core DbContext into a .net-framework app

            – Bassie
            Nov 24 '18 at 14:42



















          • Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend that Commands implements IDisposable so that I can wrap GetParts() in a using Commands()?

            – Bassie
            Nov 24 '18 at 14:30











          • Personally I would recommend just including any navigation properties you need ahead of time. I suggest using dependency injection and having your class accept an instance of dbContext and then scoping the lifetime of the dbContext to the lifetime of the request (I'm not sure how that works with WPF, but the dbContext lifetime shouldn't be the responsibility of your NetStandard library)

            – Brad M
            Nov 24 '18 at 14:36











          • I would like to try that but I'm not sure how I can inject a .net-core DbContext into a .net-framework app

            – Bassie
            Nov 24 '18 at 14:42

















          Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend that Commands implements IDisposable so that I can wrap GetParts() in a using Commands()?

          – Bassie
          Nov 24 '18 at 14:30





          Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend that Commands implements IDisposable so that I can wrap GetParts() in a using Commands()?

          – Bassie
          Nov 24 '18 at 14:30













          Personally I would recommend just including any navigation properties you need ahead of time. I suggest using dependency injection and having your class accept an instance of dbContext and then scoping the lifetime of the dbContext to the lifetime of the request (I'm not sure how that works with WPF, but the dbContext lifetime shouldn't be the responsibility of your NetStandard library)

          – Brad M
          Nov 24 '18 at 14:36





          Personally I would recommend just including any navigation properties you need ahead of time. I suggest using dependency injection and having your class accept an instance of dbContext and then scoping the lifetime of the dbContext to the lifetime of the request (I'm not sure how that works with WPF, but the dbContext lifetime shouldn't be the responsibility of your NetStandard library)

          – Brad M
          Nov 24 '18 at 14:36













          I would like to try that but I'm not sure how I can inject a .net-core DbContext into a .net-framework app

          – Bassie
          Nov 24 '18 at 14:42





          I would like to try that but I'm not sure how I can inject a .net-core DbContext into a .net-framework app

          – Bassie
          Nov 24 '18 at 14:42




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458982%2fhow-to-properly-configure-lazy-loading%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno