I have the following list in my 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:
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:
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. How would I accomplish or approach this?
Make TeamId foreign key, and access team navigation property Objective model:
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
Objective obj = _context.Objective.FirstOrDefault();
obj.Team.TeamName
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.