将EF对象序列化为JSON - ASP.NET MVC [英] Serialize EF Object into JSON - ASP.NET MVC

查看:91
本文介绍了将EF对象序列化为JSON - ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EF对象:

public class User
{

    [Key, Column("userid", TypeName = "int")]
    public Int32 UserId { get; set; }

    [Column("username", TypeName = "varchar")]
    public String UserName { get; set; }

    [Column("password", TypeName = "varchar")]
    public String Password { get; set; }

    [Column("name", TypeName = "varchar")]
    public String Name { get; set; }

    [Column("surname", TypeName = "varchar")]
    public String Surname { get; set; }

    [Column("email", TypeName = "varchar")]
    public String Email { get; set; }

    [Column("dob", TypeName = "datetime")]
    public Nullable<DateTime> Dob { get; set; }

    [Column("notes", TypeName = "nvarchar")]
    public String Notes { get; set; }

    [Column("masterentity", TypeName = "varchar")]
    public String MasterEntity { get; set; }

    [Column("propertyid", TypeName = "int")]
    public Nullable<Int32> PropertyId { get; set; }

    [Column("boardmember", TypeName = "bit")]
    public Boolean BoardMember { get; set; }

    [Column("occupiesunit", TypeName = "bit")]
    public Boolean OccupiesUnit { get; set; }

    [Column("systemuser", TypeName = "bit")]
    public Boolean SystemUser { get; set; }

    [Column("isactive", TypeName = "bit")]
    public Boolean IsActive { get; set; }


    #region Foreing Keys

    [ForeignKey("MasterEntity")]
    public virtual Entity CurrentMasterEntity { get; set; }

    #endregion

}

客户端我试图将模型序列化成JSON对象,如下所示:

In client side Im trying to serialize the model into a JSON object like this:

var jsonUser = @(Html.Raw(Json.Encode(this.Model))); 

我收到以下错误:

A circular reference was detected while serializing an object of type ....

我意识到,如果我删除 Foreing Keys Fluent API

What I realize is that if I remove the Foreing Keys Fluent API

[ForeignKey("MasterEntity")]
public virtual Entity CurrentMasterEntity { get; set; }

然后它工作完美。所以似乎与其他实体有关系的实体或对象不能使用JSON序列化。

Then it works perfect. So seems that the Entities or Objects that has relations with other Entity cant be serialized using JSON.

任何人都有一个很好的解决方法? IS EF 5.0要解决这个问题?

Anyone has a good approach of solving this? IS EF 5.0 going to solve this issue?

非常感谢。

推荐答案

只要不存在任何循环引用,您可以将序列化关系的实体。这意味着如果您有 A 指向 B B 指向 A ,它不会做。我怀疑你的 CurrentMasterEntity 将永远等于实体本身,或者可以有一个圆形图(A是B到B,B是master到A),所以您有数据错误或双向导航(大多数情况下,它的格式为 Entity Parent ICollection< Entity> Children )。如果这是数据错误,只需修复它。如果您有双向导航,您将必须选择一个将被序列化的属性,并使用 [ScriptIgnore] 标记另一个属性,以便不会

You can serialize entities with relations as long as there is no circular references between them. This means that if you have A pointing to B, and B pointing back to A, it won't do. I doubt that your CurrentMasterEntity would ever be equal to entity itself, or that there ever could be a circular graph (A is master to B, B is master to A), so you have either a data error or bi-directional navigation (most often it comes in form Entity Parent and ICollection<Entity> Children). If that is a data error, just fix it. If you have bidirectional navigation you will have to choose one property that will be serialized, and mark other one with [ScriptIgnore] so it won't be serialized.

但是如果您不需要客户端 CurrentMasterEntity ,只需将其标记为上述 [ScriptIgnore]

But if you don't really need CurrentMasterEntity on client side, just mark it with aforementioned [ScriptIgnore].

这篇关于将EF对象序列化为JSON - ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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