How to properly configure lazy loading
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 aNETStandard.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
add a comment |
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 aNETStandard.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
add a comment |
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 aNETStandard.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
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 aNETStandard.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
c# .net-core entity-framework-core lazy-loading .net-standard
asked Nov 24 '18 at 14:07
BassieBassie
3,9322051
3,9322051
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend thatCommandsimplementsIDisposableso that I can wrapGetParts()in ausing 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend thatCommandsimplementsIDisposableso that I can wrapGetParts()in ausing 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
add a comment |
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.
Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend thatCommandsimplementsIDisposableso that I can wrapGetParts()in ausing 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
add a comment |
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.
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.
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 thatCommandsimplementsIDisposableso that I can wrapGetParts()in ausing 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
add a comment |
Thanks Brad that makes sense - I didn't know it worked like that. Would you recommend thatCommandsimplementsIDisposableso that I can wrapGetParts()in ausing 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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