我的环境:
> Visual Studio 2017 RTM(使用此IDE的默认.net核心版本)
> Web API
> ASP.NET核心1.1
> MySQL通过Pomelo数据库提供商
我安装的包:
Microsoft.EntityFrameworkCorePomelo.EntityFrameworkCore.MysqL Microsoft.EntityFrameworkCore.Tools
我首先使用代码创建了3个表.
>员工:有员工名单
>申请:申请表
> EmployeeApplications:Employees和Applications的Join表
Employee和Application具有M:M关系,因此我在它们之间创建了连接表.
员工模型:
using System;using System.Collections.Generic;using System.linq;using System.Threading.Tasks;using System.ComponentModel.DataAnnotations;namespace Test.Models{ public class Employee { public int EmployeeID { get; set; } [required] public string Lname { get; set; } [required] public string Fname { get; set; } public string Title { get; set; } //Navigation Property public ICollection<EmployeeApplications> EmployeeApplications { get; set; } }}
应用模型:
using System.linq;using System.Threading.Tasks;using System.ComponentModel.DataAnnotations;namespace Teset.Models{ public class Application { public int ApplicationID { get; set; } [required] public string name { get; set; } public string Description { get; set; } //Navigation Property public ICollection<EmployeeApplications> EmployeeApplications { get; set; } }}
员工应用模型:
using System;using System.Collections.Generic;using System.linq;using System.Threading.Tasks;namespace Test.Models{ public class EmployeeApplications { public int EmployeeID { get; set; } public int ApplicationID { get; set; } public Employee Employee { get; set; } public Application Application { get; set; } }}
员工控制员:
[Produces("application/Json")][Route("API/Employees")]public class EmployeesController : Controller{ private Readonly TestContext _context; public EmployeesController(TestContext context) { _context = context; } // GET: API/Employees [httpGet] public IEnumerable<Employee> GetEmployees() { return _context.Employees; }
语境:
using Test.Models;using Microsoft.EntityFrameworkCore;namespace Test.Data{ public class TestContext : DbContext { public TestContext(DbContextoptions<TestContext> options) : base(options) { } public DbSet<Employee> Employees { get; set; } public DbSet<Application> Applications { get; set; } public DbSet<EmployeeApplications> EmployeeApplications { get; set; } protected overrIDe voID OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<EmployeeApplications>() .HasKey(c => new { c.EmployeeID,c.ApplicationID }); } }}
我尝试过使用.Include().当我跑去API / Employees时,我得到Postman的“无法得到任何回应”.
return _context.Employees.Include(e => e.EmployeeApplications);
我已经尝试了很多变化,但仍然没有运气.包含.Include()的任何变体都会失败.
电流输出样本:
[{ "employeeID": 1,"lname": "Doe","fname": "John","Title": "Senior Software Engineer","employeeApplications": null},{ "employeeID": 2,"lname": "Smith","fname": "Jack","Title": "Project Manager","employeeApplications": null}]
我尝试使用DTO并取得了一些成功.我设法用employeeApplications中的内容替换上面的null,但是后来对Employee和Applications有了null引用.
我理想的输出:
[{ "employeeID": 1,"employeeApplications": [{ "applicationID": 1,"name": "Application 1","description": "Description 1",},{ "applicationID": 2,"name": "Application 2","description": "Description 2",}]},"employeeApplications": [{ "applicationID": 2,{ "applicationID": 3,"name": "Application 3","description": "Description 3",}]}]
基本上我想提供一份“员工”列表和他们所使用的应用程序.稍后我还要提供一份应用程序列表和处理它们的员工.
编辑:上面添加了上下文文件
编辑2:我遵循Bchir Med Amine的解决方案(目前没有工作),但后来我添加到Startup.cs的ConfigureServices方法:
services.AddMvc() .AddJsonoptions( options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore );
这让我获得了有关我的JsON的更多信息,但仍未完整. JsON现在看起来像这样:
[{ "employeeID": 1,"employeeApplications": [{ "employeeID": 1,"applicationID": 1,"application": null },{ "employeeID": 1,"applicationID": 2,"application": null }]},"employeeApplications": [{ "employeeID": 2,{ "employeeID": 2,"applicationID": 3,"application": null }]}]
应用程序现在为null.我不得不使用.Include()来生成它,但是在包含应用程序之后使用.ThenInclude()不起作用.理想的情况是打印出所有员工的清单,每个员工都应该有一份申请清单.
编辑3:我错了.ThenInclude()没有工作,是Intelisense让我失望.如果我使用.ThenInclud()我得到这个JsON:
[{ "employeeID": 1,"application": { "applicationID": 1,"employeeApplications": [] } },"application": { "applicationID": 2,"employeeApplications": [] } }]},"employeeApplications": [{ "employeeID": 1,"employee": { "employeeID": 1,"employeeApplications": [{ "employeeID": 1,"application": { "applicationID": 1,"employeeApplications": [] } }] } }] } },"application": { "applicationID": 3,"employeeApplications": [] } }]}]
第一个员工对象是Ok(仍然不理想,但它可以工作),但第二个员工不是.第二个打印出员工的应用程序,然后打印出这些应用程序的员工.
编辑4:以下是我返回JsON的方式:
return _context.Employees; //Returns just employees and '[]' for employeeApplicationsreturn _context.Employees.Include(e => e.EmployeeApplications); //returns employees and their employeeApplications,but the applications of employeeApplications is emptyreturn _context.Employees.Include(e => e.EmployeeApplications).ThenInclude(ea => ea.Application); //returns everything,but continues due to circular reference解决方法 也许你必须将Virtual添加到类中
public class Application{ #region Public Constructors [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2214:DoNotCalloverrIDableMethodsInConstructors")] public Application() { EmployeeApplications = new HashSet<EmployeeApplications>(); } #endregion Public Constructors #region Public PropertIEs public int ApplicationID { get; set; } public string Description { get; set; } //Navigation Property public virtual ICollection<EmployeeApplications> EmployeeApplications { get; set; } [required] public string name { get; set; } #endregion Public PropertIEs}public class Employee{ #region Public Constructors [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2214:DoNotCalloverrIDableMethodsInConstructors")] public Employee() { EmployeeApplications = new HashSet<EmployeeApplications>(); } #endregion Public Constructors #region Public PropertIEs //Navigation Property public virtual ICollection<EmployeeApplications> EmployeeApplications { get; set; } public int EmployeeID { get; set; } [required] public string Fname { get; set; } [required] public string Lname { get; set; } public string Title { get; set; } #endregion Public PropertIEs}public class EmployeeApplications{ #region Public PropertIEs public virtual Application Application { get; set; } public int ApplicationID { get; set; } public virtual Employee Employee { get; set; } public int EmployeeID { get; set; } #endregion Public PropertIEs}总结
以上是内存溢出为你收集整理的如何从具有多对多关系的C#Web API Visual Studio 2017 RTM提供(GET)复杂(嵌套)JSON?全部内容,希望文章能够帮你解决如何从具有多对多关系的C#Web API Visual Studio 2017 RTM提供(GET)复杂(嵌套)JSON?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)