【发布时间】:2020-06-20 12:17:00
【问题描述】:
我有这个寄售实体模型。
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public bool? Deleted { get; set; }
public string Status { get; set; }
public string Services { get; set; }
public string CustomerReference { get; set; }
public string ConsignmentNote { get; set; }
public int? TotalPieces { get; set; }
...
public virtual ICollection<ConsignmentDocument> ConsignmentDocument { get; set; }
public virtual ICollection<ConsignmentLine> ConsignmentLine { get; set; }
public virtual ICollection<Legging> Legging { get; set; }
现在我在取货时面临的问题是我没有在响应正文中获得任何 ConsignmentLine、Legging、ConsignmentDocument 的数据。我在我的寄售控制器中仅使用寄售线对其进行测试。这是我的 GetConsignment() 控制器方法。
// GET: api/Consignments
[HttpGet("{id}")]
public async Task<ActionResult<Consignment>> GetConsignment(Guid id)
{
var consignment = await _context.Consignment.where(c =>c.Id == id)
.Include(conline => conline.ConsignmentLine)
.FirstOrDefault();
return consignment;
}
我的 ConsignmentLine.cs 是这样的:
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal? Length { get; set; }
public decimal? Width { get; set; }
public decimal? Height { get; set; }
public decimal? Volume { get; set; }
public int? Pieces { get; set; }
public decimal? Weigth { get; set; }
public bool? DangerousGoods { get; set; }
public string DgClass { get; set; }
public string UnNumber { get; set; }
public Guid? ConsignmentId { get; set; }
public Guid? ItemId { get; set; }
public Guid? CommodityId { get; set; }
public bool? Deleted { get; set; }
我的 Legging.cs 是这样的:
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public decimal? Cost { get; set; }
public string LegType { get; set; }
public string FromLeg { get; set; }
public string ToLeg { get; set; }
public Guid? CarrierAccount { get; set; }
public Guid? ConsignmentId { get; set; }
public bool? Deleted { get; set; }
我正在从邮递员那里测试它,它正在返回 Json 异常。那应该怎么表达呢?
【问题讨论】:
-
您能提出任何替代方案吗?
-
如回答中所说:您甚至不需要
Where。 -
但是 FirstOrDefault() 返回错误!
-
您的代码中没有
FirstOrDefault,只是告诉它“返回错误”是没有意义的。此外,你真的应该展示完整的课程。看起来您在 JSON 序列化中遇到了臭名昭著的循环引用错误。很容易找到解决此问题的方法。 -
@GertArnold 谢谢。解决了。必须安装 NewtonsoftJson 包并将其注入服务。
标签: entity-framework linq .net-core controller asp.net-core-webapi