Reputation: 3806
These are my model and I am using entity framework code first approach.
public class Respondent
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int RespondentId { get; set; }
public User Requester { get; set; }
public User Provider { get; set; }
public string Role { get; set; }
[NotMapped]
public ICollection<User> Providers { get; set; }
}
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public int UPI { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public bool IsPublic { get; set; }
[NotMapped]
public string Profile_Pic { get; set; }
[NotMapped]
public string Role { get; set; }
[NotMapped]
public List<string> Roles { get; set; }
}
Now I want to get all respondents by using following web api method but i am not getting correct result set and it is showing null values for Provider and requester and only returning respondent id.
public IQueryable<Respondent> GetRespondents()
{
return db.Respondents;
}
Upvotes: 0
Views: 80
Reputation: 1909
You can use the Include()
function to load related data through your model's navigation properties.
Something like this.
// GET: api/Respondents
public IQueryable<Respondent> GetRespondents()
{
return db.Respondents.
Include(user=>user.Requester)
.Include(pro=pro.Providers).ToList();
}
Note that if you are using EntityFramework Core, you need the following namespace
using Microsoft.EntityFrameworkCore;
Otherwise you need:
using System.Data.Entity;
Upvotes: 1