简体   繁体   English

实体框架代码第一个一对多关系使用Web API返回数据

[英]Entity Framework Code First One to Many relationship return data using web api

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. 现在,我想通过使用以下Web api方法来获取所有答复者,但是我没有得到正确的结果集,并且它显示Provider和Requester的空值,并且仅返回响应者ID。

        public IQueryable<Respondent> GetRespondents()
        {
            return db.Respondents;
        }

You can use the Include() function to load related data through your model's navigation properties. 您可以使用Include()函数通过模型的导航属性加载相关数据。

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 请注意,如果您使用的是EntityFramework Core,则需要以下名称空间

using Microsoft.EntityFrameworkCore;

Otherwise you need: 否则,您需要:

using System.Data.Entity; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Entity Framework 5.0代码首先是一对一和一对多的关系 - Entity Framework 5.0 code first one to one and one to many relationship 无需代码优先模型创建的一对多关系实体框架 - One To Many relationship entity framework without Code First Model Creation 实体框架-代码优先-共享主键一对多关系 - Entity Framework - Code first - One to Many relationship with shared primary key Code First Entity Framework不会创建一对多关系 - Code First Entity Framework doesn't create one to many relationship 具有 1 对多对多关系的 Web API 和实体框架 - Web API and Entity Framework with a 1 to many to many relationship 实体框架核心 api 保存一对多关系数据 - entity framework core api save one to many relationship data 如何使用Entity Framework 4.1 Code First强制数据库中一对多关系的一对一关系 - How to use Entity Framework 4.1 Code First to force a one to one relationship for a one to many relationship in the database 实体框架Web.API中的代码优先一对多关系 - entity framework Code First One-to-Many relationships in Web.API 使用Fluent API在实体框架中创建一对多关系 - Creating one to many relationship in Entity Framework using Fluent API 实体框架代码优先的一对一关系 - One to One relationship in Entity Framework Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM