English 中文(简体)
实体框架目标
原标题:Convert Entity Framework Object to JSON Object

I m 试图根据我的实体类型( SYSTEM_AUDIT_SHEET

SYSTEM_AUDIT_SHEET sheet = ctx.SYSTEM_AUDIT_SHEET
                              .Where(s => s.SYSTEM_KEY == system_key_dec)
                              .Select(s => s)
                              .OrderByDescending(s => s.AUDIT_SHEET_VERSION)
                              .First();

HttpContext.Current.Response.Write(serializer.Serialize(sheet));

但我有以下错误:

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

为什么我能把该实体改为“智者”?

最佳回答

你们不能将物体转换为避风港,因为这样做会造成长篇大笑。

例如,下列假体编码为固定工作,因为它建立了循环参考(Dog >> Bone >> Dog...):

class Dog {
    private Bone myBone;
    public Dog() {
        myBone = new Bone(this);
    }
}

class Bone {
    private Dog buriedBy;
    public Bone(Dog d) {
        buriedBy = d;
    }
}

似乎有一些很好的解决办法,即通过 go格文参考。 见上两部分流出环节。

问题回答

The problem is probably that your SYSTEM_AUDIT_SHEET either contains a property that references instances of type SYSTEM_AUDIT_SHEET, or it contains a property that points to objects that have pointers to SYSTEM_AUDIT_SHEET instances. Serializing such a circle of pointers would result in a serialization process that would never end.

您将需要将<代码>SYSTEM_AUDIT_SHEET改为在进行序列化之前没有(直接或间接)参照的类型。 您可以创建一种品牌新类型和书写代码,以在以下表格中显示这种类型:(AutoMapper, 可能在此有手。 然而,我倾向于认为,在大多数情况下,使用匿名类型更容易:

SYSTEM_AUDIT_SHEET sheet = /*some sheet*/
var json = new {
  sheet.Id,
  sheet.RevisionNumber,
  sheet.Title
};
return serializer.Serialize(json);

http://www.ohchr.org。

如果你想使用自动申请器,并假定贵方书看起来像什么。

class SYSTEM_AUDIT_SHEET 
{
    public int Id { get; set; }
    public SYSTEM_AUDIT_SHEET SomeOtherAuditSheet { get;set;}
    public string Title { get;set;}
}

你们可以创造这样的环境。

class JSON_SYSTEM_AUDIT_SHEET
{
    public int Id { get; set; }
    public int SomeOtherAuditSheetsId { get;set;}
    public string Title { get;set;}
}

当申请开始时(例如,在申请中——Start),你召集了“自动申请”:

AutoMapper.Mapper.CreateMap<SYSTEM_AUDIT_SHEET, JSON_SYSTEM_AUDIT_SHEET>()
  .ForMember(dest => dest.SomeOtherAuditSheetsId, opt => opt.MapFrom(src => src.SomeOtherAuditSheet.Id));

<代码>Id和Title 特性将直接从 SYSTEM_AUDIT_SHEETJSON_SYSTEM_AUDIT_SHEET ,因为它们有两种类型相同的名称。 财产<代码>Some OtherAuditSheetsId需要特殊配置,因为不存在来源类型上的确切名称的财产。

当你想转换时 海关数据 您:

return AutoMapper.Mapper.Map<SYSTEM_AUDIT_SHEET , JSON_SYSTEM_AUDIT_SHEET >(sheet);

您不妨查阅。 AutoMapper sngtening profiles

希望这一帮助。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签