0

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;
        }

1 Answer 1

1

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; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.