[英]How do I get a value from a foreign key to populate a list with an associated value?
I have the following list in my ViewModel:我的 ViewModel 中有以下列表:
public List<Objective> Objectives { get; set; }
I also have the following repository method:我还有以下存储库方法:
public async Task<List<Objective>> GetAll()
{
return await _context.Objective.ToListAsync();
}
Finally I have the following method in a service which returns a ViewModel:最后,我在返回 ViewModel 的服务中有以下方法:
public async Task<ObjectivesViewModel> GetViewModel()
{
var viewModel = new ObjectivesViewModel()
{
Objectives = await _objectivesRepository.GetAll()
};
return viewModel;
}
All of this works as expected.所有这些都按预期工作。
My Objective Model looks like this:我的目标模型如下所示:
public string ReferenceCode { get; set; }
public int TeamId { get; set; }
public string Description { get; set; )
With the TeamId Model looking like this: TeamId 模型如下所示:
public int TeamId { get; set; }
public string TeamName { get; set; }
As it stands, the repository returns all, as it should, however what I'd like to do is to add the TeamName associated with the TeamId.就目前而言,存储库应该返回所有内容,但是我想做的是添加与 TeamId 关联的 TeamName。 How would I accomplish or approach this?
我将如何完成或解决这个问题?
Make TeamId foreign key, and access team navigation property Objective model:制作 TeamId 外键,并访问团队导航属性目标模型:
public string ReferenceCode { get; set; }
public int TeamId { get; set; }
[ForeignKey("TeamId ")]
public Team Team { set; get; }
public string Description { get; set; )
now you can access teamId or teamName现在您可以访问 teamId 或 teamName
Objective obj = _context.Objective.FirstOrDefault();
obj.Team.TeamName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.