0

I have an EF Object:

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

}

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

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

Im getting the following error:

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

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

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

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

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

Thanks a lot.

VAAA
  • 14,531
  • 28
  • 130
  • 253

1 Answers1

0

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.

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

Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35
  • i have edited your answer, see my comments. Thanks a lot and keep in touch. – VAAA Jul 24 '12 at 19:05
  • Im returning the entity from the controller like this: var userObj = db.Users.Where(u => u.UserId == id).FirstOrDefault(); return PartialView(userObj); Is this ok? – VAAA Jul 24 '12 at 19:07
  • I've rejected your edit, sorry, it should be as another comment. But you say you add `ScriptIgnore` and nothing happens. I suspect one of two things: either there's more to your class that you show us, or Json serialization gets confused because of the proxy EF builds on top of your class. Do you really need it? If not, try removing `virtual` modifiers from your class *and* putting `ScriptIgnore`. – Sergei Rogovtcev Jul 24 '12 at 19:11
  • Yes is something I need actually, what I came to was to create a Clone class without any of the Fluent API stuff (a clean class) and then using AutoMapper to map the EF object to this clean class. This way works.. but would be great if EF let you do that directly. – VAAA Jul 24 '12 at 19:22
  • What do you need EF proxy for? – Sergei Rogovtcev Jul 24 '12 at 19:27