2011-07-14 69 views
1

我有EF Code First的问题,它抱怨生成的代理中的循环引用。也许我们有一个会导致EF在运行中创建循环引用的约定冲突?当直接传递给JSON序列化程序时,数据集不会产生错误。实体框架导致循环引用?

/* Assume all dependencies and namespaces are referenced and used */ 

///the base object 
public class A { 
    [Key] 
    public int AId { set; get; } 

    //the tree 
    public B MyB { set; get; } 
} 

//the tree nodes 
public class B { 
    [Key] 
    public int BId { set; get; } 
    public int AId { set; get; } 
    public int ParendId { set; get; } 
    public virtual ICollection<B> Children { set; get; } 
} 

///the context 
public class ABContext : DbContext { 
    public DbSet<A> As { set; get; } 
    public DbSet<B> Bs { set; get; } 
} 

///later in a controller... 
[HttpGet] 
public JsonResult Get(string sid) 
{ 
    int id = int.Parse(sid); 
    using (ABContext abc = new ABContext()) { 
     A a = abc.As.Where(i=>i.AId==id).Single(); 
     return Json(a, JsonRequestBehavior.AllowGet); 
    } 
} 

想法和意见都非常欢迎!

谢谢 亚历山大·布雷维格

+0

你的树节点是否有循环引用?并尝试将'ParendId'设为可空int – Eranga

回答

相关问题