【问题标题】:How to get the JSON array as respone from asp.net core web api如何从 asp.net core web api 获取 JSON 数组作为响应
【发布时间】:2020-04-27 17:04:57
【问题描述】:

大家好,我正在尝试使用 LINQ 和实体框架在一个 SQL 查询中从多个表中提取数据。我需要从数据库中获取详细信息。有多个区域。

1) 获取所有区域。 2) 获得该地区的所有项目。 3) 获取项目中的所有部分。

我需要根据区域 ID 从每个区域获取所有项目详细信息。

public partial class Regions
    {
        public Regions()
        {
            Projects = new HashSet<Projects>();
            RegionUserRelation = new HashSet<RegionUserRelation>();
        }

        public int RegionId { get; set; }
        [Required]
        public string RegionName { get; set; }
        [Required]
        public string RegionFullname { get; set; }
        [Required]
        public int RegionTypeId { get; set; }
        public string RegionMisc { get; set; }

        public RegionType RegionType { get; set; }
        public ICollection<Projects> Projects { get; set; }
        public ICollection<RegionUserRelation> RegionUserRelation { get; set; }
    }

public partial class Projects
    {
        public Projects()
        {
            Sections = new HashSet<Sections>();
        }

        public Guid ProjectId { get; set; }
        [Required]
        public string ProjectName { get; set; }
        [Required]
        public string ProjectCode { get; set; }
        public string ProjectDesc { get; set; }
        [Required]
        public int RegionId { get; set; }
        [Required]
        public int EpsgId { get; set; }

        public Epsg Epsg { get; set; }
        public Regions Region { get; set; }
        public ICollection<Sections> Sections { get; set; }
    }


public partial class Sections
    {
        public Sections()
        {
            Flights = new HashSet<Flights>();
        }

        public Guid SectionId { get; set; }
        [Required]
        public string SectionName { get; set; }
        public string SectionDesc { get; set; }
        [Required]
        public Guid ProjectId { get; set; }

        public Projects Project { get; set; }
        public ICollection<Flights> Flights { get; set; }
    }

预期输出是:

{ "Regions" : 
  [ { 
        "region_id" : "2012",
        "region_name" : "region1",
        "region_fullname" : "21700",
        "region_type" : "MERCH",
            "Projects" : [ 
               {     "project_id" : 2341,
                     "project_name " : "DXY",
                     "project_code " : "21700",
                     "sections" : [
                                { "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis},
                                { "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2},
                                { "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3}
                            ]
                },
            "Projects" : [ 
               {     "project_id" : 1321,
                     "project_name " : "Proj_2",
                     "project_code " : "01700",
                     "sections" : [
                                { "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis},
                                { "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2},
                                { "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3}
                            ]
                },
            ]
  },
  { 
        "region_id" : "2013",
        "region_name" : "region3",
        "region_fullname" : "21330",
        "region_type" : "MERCH",
            "Projects" : [ 
               {     "project_id" : 2341,
                     "project_name " : "DXY",
                     "project_code " : "21700",
                     "sections" : [
                                { "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis},
                                { "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2},
                                { "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3}
                            ]
                },
            "Projects" : [ 
               {     "project_id" : 1321,
                     "project_name " : "Proj_2",
                     "project_code " : "01700",
                     "sections" : [
                                { "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis},
                                { "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2},
                                { "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3}
                            ]
                },
            ]
  },
  { 
        "region_id" : "2012",
        "region_name" : "region2",
        "region_fullname" : "24300",
        "region_type" : "MERCH",
            "Projects" : [ 
               {     "project_id" : 2341,
                     "project_name " : "DXY",
                     "project_code " : "21700",
                     "sections" : [
                                { "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis},
                                { "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2},
                                { "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3}
                            ]
                },
            "Projects" : [ 
               {     "project_id" : 1321,
                     "project_name " : "Proj_2",
                     "project_code " : "01700",
                     "sections" : [
                                { "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis},
                                { "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2},
                                { "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3}
                            ]
                },
            ]
  },
] }

我试过这个,但它只给出了该地区的一个记录。我需要所有地区和地区的所有项目

IList<Regions> ches = entities.Regions.Include
                        (c => c.Projects).ToList();
                    return Ok(ches);

输出

[{"regionId":1,"regionName":"Mitte","regionFullname":"Region Mitte","regionTypeId":1,"regionMisc":"","regionType":null,"projects":[{"projectId":"9dd284be-004d-42ee-8e21-3acbb240c488","projectName":"EÜ Stockstadt","projectCode":"B-MI01007P","projectDesc":"","regionId":1,"epsgId":32632,"epsg":null

【问题讨论】:

    标签: entity-framework asp.net-core asp.net-mvc-4 asp.net-web-api entity-framework-core


    【解决方案1】:

    我已经尝试过这个,但它只给出了该地区的一个记录。我需要所有地区和地区的所有项目

    确保您的数据库包含多条记录。

    型号:

    public partial class Regions
    {
        public Regions()
        {
            Projects = new HashSet<Projects>();
        }
        [Key]
        public int RegionId { get; set; }
        [Required]
        public string RegionName { get; set; }
        [Required]
        public string RegionFullname { get; set; }
        [Required]
        public int RegionTypeId { get; set; }
        public string RegionMisc { get; set; }
    
        public ICollection<Projects> Projects { get; set; }
    }
    
    public partial class Projects
    {
        public Projects()
        {
            Sections = new HashSet<Sections>();
        }
        [Key]
        public Guid ProjectId { get; set; }
        [Required]
        public string ProjectName { get; set; }
        [Required]
        public string ProjectCode { get; set; }
        public string ProjectDesc { get; set; }
        [Required]
        public int RegionId { get; set; }
        public Regions Region { get; set; }
        public ICollection<Sections> Sections { get; set; }
    }
    
    public partial class Sections
    {
        [Key]
        public Guid SectionId { get; set; }
        [Required]
        public string SectionName { get; set; }
        public string SectionDesc { get; set; }
        [Required]
        public Guid ProjectId { get; set; }
    
        public Projects Project { get; set; }
    }
    

    控制器:

    从您预期的 json 中,您还想加载与 Projects 相关的 Sections 模型:

    [Route("api/[controller]")]
    [ApiController]
    public class TestsController : ControllerBase
    {
        private readonly YourDbContext _context;
    
        public TestsController(YourDbContext context)
        {
            _context = context;
        }
    
        // GET: api/Tests
        [HttpGet]
        public async Task<ActionResult<Regions>> GetTest()
        {
    
            IList<Regions> ches = _context.Regions.Include
                        (c => c.Projects).ThenInclude(c=>c.Sections).ToList();
            return Ok(ches);
        }
    }
    

    Startup.cs:

    对于 asp.net core 2.2:

    services.AddMvc()
            .AddJsonOptions(options =>
                 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore)
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    

    对于 asp.net core 3.x:

    services.AddControllers().AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 2020-10-04
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      相关资源
      最近更新 更多