【发布时间】:2018-11-24 14:07:18
【问题描述】:
在下面的视图模型中,我可以看到part.CountryCode 始终是null。
如果我使用db.Parts.Include(p => p.CountryCode),我可以获取数据,但我现在想设置延迟加载。有谁知道我错过了什么?
我正在使用:
-
Microsoft.EntityFrameworkCore (2.1.4)在NETStandard.Library(2.0.3) - UI 是带有 .NET 4.6.1 的 WPF
视图模型
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