我试图序列化从实体数据模型.edmx自动生成的POCO类,当我使用时
JsonConvert.SerializeObject
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误发生类型System.data.entity检测到的自引用循环.
我该如何解决这个问题?
我在4个表中为我的网站提供了一个mssql数据库.
我用这个时:
public static string GetAllEventsForJSON()
{
using (CyberDBDataContext db = new CyberDBDataContext())
{
return JsonConvert.SerializeObject((from a in db.Events where a.Active select a).ToList(), new JavaScriptDateTimeConverter());
}
}
Run Code Online (Sandbox Code Playgroud)
该代码导致以下错误:
Newtonsoft.Json.JsonSerializationException:为类型为"DAL.CyberUser"的属性"CyberUser"检测到自引用循环.路径'[0] .EventRegistrations [0] .CyberUser.UserLogs [0]'.
我的理解是,当您在对象模型中定义循环引用时,您可以使用此设置来解决出现以下错误的问题:
JsonException: 检测到不支持的可能的对象循环。这可能是由于循环或对象深度大于最大允许深度 32。
但是,我无法成功实施它以使其正常工作。如果有人可以提供有关需要做什么的详细说明,将不胜感激!
我想将应用程序切换为使用 Newtonsoft.JSON,但从我读到的内容来看,这在 Blazor WebAssembly 应用程序中是不可行的?
2020 年 12 月 12 日更新
我在试图弄清楚如何实现 ReferenceHandler.Preserve 时发现的最接近的文章是这些:
https://github.com/dotnet/runtime/issues/42584
https://github.com/dotnet/aspnetcore/issues/28286
基于这些文章,我尝试实施以下解决方案,但都没有奏效......
第一次尝试我在我的 Server 项目的 Startup.cs 类中实现了以下代码:
services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
});
Run Code Online (Sandbox Code Playgroud)
第二次尝试我在我的服务器项目的 Startup.cs 类中实现了以下代码:
services.AddControllersWithViews(options =>
{
options.OutputFormatters.RemoveType<SystemTextJsonOutputFormatter>();
options.OutputFormatters.Add(new SystemTextJsonOutputFormatter(new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
ReferenceHandler = ReferenceHandler.Preserve
}));
});
Run Code Online (Sandbox Code Playgroud)
更新 12/12/2020 上午 11:12 CST
在将我的服务器项目更改为面向 .NET 5 并尝试上面的两个代码选项后,我现在在我的应用程序的每个页面上都收到以下类型的错误:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:JSON 值无法转换为 BusinessManager.Shared.Models.EmploymentBenefit[]。路径:$ | 行号:0 | 字节位置内联:1。
重现原始问题的步骤
创建一个新的 Blazor WebAssembly 应用程序
在 Shared 项目中定义一个具有子对象集合的父类,如下所示:
public virtual List<Child> Children{ get; …Run Code Online (Sandbox Code Playgroud) 我创建了一个Web Api来保存数据库中的新产品和评论.下面是WebApi代码:
// POST api/Products
[ResponseType(typeof(Product))]
public IHttpActionResult PostProduct(Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Products.Add(product);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = product.ProductId }, product);
}
Run Code Online (Sandbox Code Playgroud)
产品类别 -
public class Product
{
public int ProductId { get; set; }
[Required]
public string Name { get; set; }
public string Category { get; set; }
public int Price { get; set; }
//Navigation Property
public ICollection<Review> Reviews { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
复习课程 -
public class Review
{ …Run Code Online (Sandbox Code Playgroud) 在使用ASP.NET Web API进行JSON序列化期间,避免使用Entity Framework 6进行循环引用的可能性有哪些?
我首先为数据库Entity Framework 6生成了edmx(实体数据模型)文件。我尝试使用ASP.NET Web API构建API。当我尝试在控制器中返回JSON对象时,由于循环引用,我遇到了序列化的运行时异常。
确实,当我仔细检查数据库和实体时,我看到一个实体包含一个列表,另一个实体包含了先前实体的列表。假设我有一个包含作者的图书实体,每个作者实体均包含一个图书列表。这在相对数据库中很常见,但无法在JSON序列化中解决(或无法为.NET序列化器解决)。
我不想更改数据库,但已准备好将错误的列表分解为实体或edmx文件。我能做什么?
我尝试过的
我已经尝试过由创建新模型或实体以及使用映射工具(http://www.codeproject.com/Articles/292970/Avoiding-Circular-Reference-for-Entity-in-JSON-Ser或Shawn Wildermuth在Pluralsight上解释的解决方案)。
与实际解决方案相比,此解决方案听起来更像是一种解决方法。它应该在edmx文件或Entity Framework中存在一些内容,以告诉JSON序列化程序哪些会导致循环引用,哪些可以并且必须序列化,哪些不能序列化,对吗?