循环引用在将对象序列化为 JSON 时检测到异常 [英] Circular reference detected exception while serializing object to JSON

查看:35
本文介绍了循环引用在将对象序列化为 JSON 时检测到异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在这个中提到的帖子,我在序列化实体框架代理时收到 Json 序列化错误:

Just as mentioned in this post, I am getting a Json serialization error while serializing an Entity Framework Proxy:

在序列化类型对象时检测到循环引用'System.Data.Entity.DynamicProxies.PurchaseOrder_446B939192F161CDBC740067F174F7A6059B0F9C0EEE68CD3EBBD63CF9AF5BD0'.

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.PurchaseOrder_446B939192F161CDBC740067F174F7A6059B0F9C0EEE68CD3EBBD63CF9AF5BD0'.

但不同之处在于,我的实体没有循环引用,它出现在我们的生产环境中.本地一切正常...

But the difference is, I don't have a circular reference in my entities, and it only occurs in our production environment. Locally everything works fine...

我的实体:

public interface IEntity
{
    Guid UniqueId { get; }
    int Id { get; }
} 

public class Entity : IEntity
{
    public int Id { get; set; }
    public Guid UniqueId { get; set; }
}

public class PurchaseOrder : Entity
{
    public string Username { get; set; }
    public string Company { get; set; }

    public string SupplierId { get; set; }
    public string SupplierName { get; set; }

    public virtual ICollection<PurchaseOrderLine> Lines { get; set; }
}

public class PurchaseOrderLine : Entity
{
    public string Code { get; set; }
    public string Name { get; set; }
    public decimal Quantity { get; set; }
}

我的 PurchaseOrderController 上的 GetCurrent 操作抛出异常:

The GetCurrent action on my PurchaseOrderController throwing the exception:

public class PurchaseOrderController : Controller
{
    private readonly IUnitOfWork _unitOfWork;

    public PurchaseOrderController(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public JsonResult GetCurrent()
    {
        return Json(EnsurePurchaseOrder(), JsonRequestBehavior.AllowGet);
    }

    private PurchaseOrder EnsurePurchaseOrder()
    {
        var company = RouteData.GetRequiredString("company");
        var repository = _unitOfWork.GetRepository<PurchaseOrder>();

        var purchaseOrder = repository
                .Include(p => p.Lines)
                .FirstOrDefault
                (
                    p => p.Company == company && 
                         p.Username == User.Identity.Name
                );

        if (purchaseOrder == null)
        {
            purchaseOrder = repository.Create();
            purchaseOrder.UniqueId = Guid.NewGuid();
            purchaseOrder.Company = company;
            purchaseOrder.Username = User.Identity.Name;
            _unitOfWork.SaveChanges();
        }

        return purchaseOrder;
    }
}

推荐答案

您的 POCO 实体是完全可序列化的.您的问题是 EF 运行时为您创建的动态代理通常不是.您可以将 context.Configuration.ProxyCreationEnabled 设置为 false ,但随后您将丢失延迟加载.我强烈建议您使用支持 EF 实体序列化的 Json.NET:

Your POCO entities are perfectly serializable. Your problem is that the dynamic proxies the EF runtime creates for you are usually not. You can set the context.Configuration.ProxyCreationEnabled to false but then you lose lazy loading. My strong recommendation to you is to use Json.NET which supports serialization for EF entities:

ADO.NET Entity Framework 支持意外添加到 Json.NET

流行的 .NET 高性能 JSON 框架

这篇关于循环引用在将对象序列化为 JSON 时检测到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆