【发布时间】:2020-09-18 18:18:28
【问题描述】:
我想根据下面的代码将数据库表与 EF Core 3.1 连接起来。 问题是,ModellNavigation 和 ManufacturerNavigation 返回 null。 我究竟做错了什么?请帮我解决这个问题。
public IEnumerable<ViewModel> GetAll()
{
List<ViewModel> models = new List<ViewModel>();
foreach(Detail detail in _context.Detail)
{
ViewModel viewModel = new ViewModel
{
ID = detail.DetailId,
Manufacturer = detail.ModellNavigation.ManufacturerNavigation.Name,
Modell = detail.ModellNavigation.Name,
Consumption = detail.Consumption,
Color = detail.Color,
Year = detail.Year,
Horsepower = detail.Horsepower
};
models.Add(viewModel);
}
return models;
}
【问题讨论】:
-
能否详细说明您的实体配置?
-
public partial class Detail { public int DetailId { get; set; } public decimal Consumption { get; set; } public string Color { get; set; } public DateTime Year { get; set; } public int Horsepower { get; set; } public int Modell { get; set; } public virtual Modell ModellNavigation { get; set; } } -
我认为您的配置不足,这就是 modelnavigation 为空的原因。请检查我的答案。
-
您的关系配置可能正确,但您仍需要启用延迟加载或通过
.Include显式加载 -
将该代码添加到问题中,而不是作为评论
标签: .net-core entity-framework-core