【发布时间】:2020-05-19 02:55:20
【问题描述】:
检测到类型为“Entities.Models.ConsignmentDetails”的自引用循环。路径 'consignmentDetails[0].Consignee.ConsignmentDetails'.
我可能尝试过使用互联网上的每个解决方案进行锻炼,但无法找到我出错的地方,或者我已经完全搞砸了
在调试时,我得到的是从收货人到寄售详细信息然后再从收货人到寄售详细信息的无限循环数据,并且它继续
我无法更改我的模型类,因为我需要在获取 consignee 时获取 consignmentdetails,并且在获取 consignmentdetails 时我还需要 consignee
这是我的寄售详情模型
namespace Entities.Models
{
//[ScriptIgnore]
[DataContract(IsReference = true)]
public partial class ConsignmentDetails
{
public int ConsignmentDetailsId { get; set; }
public int DocketNo { get; set; }
public int? ConsignmentValue { get; set; }
public int? Freight { get; set; }
public int? Aoc { get; set; }
public int? ConsigneeId { get; set; }
.
.
public virtual Consignees Consignee { get; set; }
}
}
这是我的收货人模型
namespace Entities.Models
{
[DataContract(IsReference = true)]
public partial class Consignees
{
public int ConsigneeId { get; set; }
.
.
public virtual ICollection<ConsignmentDetails> ConsignmentDetails { get; set; }
}
}
我正在尝试做的内部控制器
public IActionResult GetConsigneeWithDetails(int id)
{
try
{
var consignee = _repository.consignees.GetConsigneesWithDetails(id);
if (consignee == null)
{
_logger.LogError($"Consignee With Id: {id} hasn't been foud inn the db");
return NotFound();
}
else
{
_logger.LogInfo($"Returned Consignee with Details for id: {id}");
var consigneeResult = _mapper.Map<ConsigneeDTO>(consignee);
JsonConvert.SerializeObject(consigneeResult);
return Ok(consigneeResult);
}
}
catch(Exception ex)
{
_logger.LogError($"SOmething Went Wrong inside GetConsigneeWithDetails action: {ex.Message}");
return StatusCode(500, "Internal Server Error");
}
}
这就是我的控制器获取数据的方式
public Consignees GetConsigneesWithDetails(int id)
{
return FindByCondition(consignee => consignee.ConsigneeId.Equals(id)).Include(d => d.ConsignmentDetails).FirstOrDefault();
}
上面的 consignmentdetails 模型有 20 多个属性,我得到的响应是
{
"consigneeId": 5,
"consigneeName": "ABC",
"consigneeGstno": "AFLPG-8314-H",
"consignmentDetails": [
{
"$id": "1"
}
]
}
【问题讨论】:
标签: asp.net-core asp.net-core-webapi