Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架使用web api编码第一个一对多关系返回数据_C#_Asp.net_Entity Framework_Asp.net Web Api - Fatal编程技术网

C# 实体框架使用web api编码第一个一对多关系返回数据

C# 实体框架使用web api编码第一个一对多关系返回数据,c#,asp.net,entity-framework,asp.net-web-api,C#,Asp.net,Entity Framework,Asp.net Web Api,这些是我的模型,我使用实体框架代码优先的方法 public class Respondent { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int RespondentId { get; set; } public User Requester { get; set; } public User Provider { get; set; }

这些是我的模型,我使用实体框架代码优先的方法

 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; }
    }
现在我想使用下面的WebAPI方法获取所有响应者,但我没有得到正确的结果集,它显示提供者和请求者的空值,并且只返回响应者id

        public IQueryable<Respondent> GetRespondents()
        {
            return db.Respondents;
        }
可以使用Include函数通过模型的导航属性加载相关数据

像这样的

// GET: api/Respondents


public IQueryable<Respondent> GetRespondents() 
{ 
return db.Respondents.
Include(user=>user.Requester)
    .Include(pro=pro.Providers).ToList();
}
否则,您需要:

using System.Data.Entity; 
using System.Data.Entity;