.net core EF核心3.1导航属性
根据下面的代码,我想用EF Core 3.1连接数据库表。 问题是ModelNavigation和ManufacturerNavigation返回null。 我做错了什么?请帮我做这个.net core EF核心3.1导航属性,.net-core,entity-framework-core,.net Core,Entity Framework Core,根据下面的代码,我想用EF Core 3.1连接数据库表。 问题是ModelNavigation和ManufacturerNavigation返回null。 我做错了什么?请帮我做这个 public IEnumerable<ViewModel> GetAll() { List<ViewModel> models = new List<ViewModel>(); foreach(De
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 IEnumerable GetAll()
{
列表模型=新列表();
foreach(在_context.Detail中详细说明)
{
ViewModel ViewModel=新ViewModel
{
ID=detail.DetailId,
制造商=detail.modelnavigation.ManufacturerNavigation.Name,
Modell=detail.ModellNavigation.Name,
消耗=细节。消耗,
颜色=细节。颜色,
年份=细节。年份,
马力=细节。马力
};
models.Add(viewModel);
}
收益模型;
}
您需要在EntityFramework配置中定义细节和模型导航的关系,以便在访问细节模型时加载模型导航。
请检查此链接:
您需要在EntityFramework配置中定义细节和模型导航的关系,以便在访问细节模型时加载模型导航。 请检查此链接:
在此代码段中,modell返回null,尽管它使用lasy加载! 再次感谢你的帮助,我真的很感激!(请原谅我的英语,我不是本地人!)
[HttpGet]
公共IEnumerable getAllCars()
{
列表视图=新列表();
foreach(制造商上下文中的制造商)
{
车视图车=新车视图();
car.Manufacturer=Manufacturer.Name;
foreach(Modell Modell在manufacturer.Modell中)
{
car.Modell=Modell.Name;
foreach(模型详细信息中的详细信息)
{
car.ID=detail.DetailId;
car.consumpion=detail.Consumption;
car.Color=detail.Color;
car.Year=细节.Year;
汽车。马力=细节。马力;
}
}
汽车视图。添加(汽车);
}
返回车视图;
}
在此代码段中,modell返回null,尽管它使用lasy加载!
再次感谢你的帮助,我真的很感激!(请原谅我的英语,我不是本地人!)
[HttpGet]
公共IEnumerable getAllCars()
{
列表视图=新列表();
foreach(制造商上下文中的制造商)
{
车视图车=新车视图();
car.Manufacturer=Manufacturer.Name;
foreach(Modell Modell在manufacturer.Modell中)
{
car.Modell=Modell.Name;
foreach(模型详细信息中的详细信息)
{
car.ID=detail.DetailId;
car.consumpion=detail.Consumption;
car.Color=detail.Color;
car.Year=细节.Year;
汽车。马力=细节。马力;
}
}
汽车视图。添加(汽车);
}
返回车视图;
}
像您这样的枚举将为每个循环生成一个查询,如果您还添加了延迟加载,它将生成更多的查询和复杂性
您应该直接在EF Core中投影查询,这样只会生成一个数据库查询
public IEnumerable<ViewModel> GetAll()
{
return _context.Detail.Select(
d => 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
}
).ToList();
}
public IEnumerable GetAll()
{
return\u context.Detail.Select(
d=>newviewmodel()
{
ID=detail.DetailId,
制造商=detail.modelnavigation.ManufacturerNavigation.Name,
Modell=detail.ModellNavigation.Name,
消耗=细节。消耗,
颜色=细节。颜色,
年份=细节。年份,
马力=细节。马力
}
).ToList();
}
像您这样的枚举将为每个循环生成一个查询,如果您还添加了延迟加载,它将生成更多的查询和复杂性
您应该直接在EF Core中投影查询,这样只会生成一个数据库查询
public IEnumerable<ViewModel> GetAll()
{
return _context.Detail.Select(
d => 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
}
).ToList();
}
public IEnumerable GetAll()
{
return\u context.Detail.Select(
d=>newviewmodel()
{
ID=detail.DetailId,
制造商=detail.modelnavigation.ManufacturerNavigation.Name,
Modell=detail.ModellNavigation.Name,
消耗=细节。消耗,
颜色=细节。颜色,
年份=细节。年份,
马力=细节。马力
}
).ToList();
}
您可以为详细信息显示实体配置吗?公共部分类详细信息{public int DetailId{get;set;}公共十进制消耗{get;set;}公共字符串颜色{get;set;}公共日期时间年{get;set;}公共int马力{get;set;}公共int model{get;set;}公共虚拟模型导航{get;set;}
我认为您的配置不足,这就是modelnavigation为空的原因。请检查我的答案。您的关系配置可能正确,但您仍然需要启用延迟加载或通过显式加载。包括
将该代码添加到问题中,而不是作为注释。您可以显示deta的实体配置吗il?public分部类详细信息{public int DetailId{get;set;}公共十进制消耗{get;set;}公共字符串颜色{get;set;}公共D