System.InvalidOperationException:检测到循环引用 [英] System.InvalidOperationException: A circular reference was detected

查看:156
本文介绍了System.InvalidOperationException:检测到循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: A circular reference was detected while serializing an object of type projectnetwork.Models.Branch.
   at System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String name, String ns, Object o, Boolean writePrefixed, XmlSerializerNamespaces xmlns)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_Branch(String n, String ns, Branch o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_Company(String n, String ns, Company o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_Branch(String n, String ns, Branch o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write8_Branch(Object o)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()

几个小时前,到目前为止,我无法解决这个问题我真的很累,在我疯了之前的最后机会:)我可以找到解决这个问题的人吗?
我想做的是返回类型为branch的对象,该对象是孩子的包括其父公司Company的对象。

A few hours ago and so far I could not solve this problemو I am really tired and for the last chance before I became a crazy :) can I find someone who solved this problem ? what I'm trying to do is return object of type branch which is the child include the object of his parent which is Company.


  • 分支类

  • Branch class

public partial class Branch
{
    public Branch()
    {
        this.Customers = new List<Customer>();
    }

        public int BranchID { get; set; }
        public int CompanyID { get; set; }
        public string BranchName { get; set; }
        public string BranchShortName { get; set; }
        public string BranchAddress { get; set; }
        public string BranchPhone { get; set; }
        public string BranchEmail { get; set; }
        public string BranchFax { get; set; }
        public Nullable<float> BranchLimit { get; set; }
        public bool RecordState { get; set; }
        public virtual Company Company { get; set; }        
    }


  • 公司类别

  • Company class

    public partial class Company
    {
        public Company()
        {
            this.Branches = new List<Branch>();
        }
    
        public int CompanyID { get; set; }
        public string CompanyName { get; set; }
        public string CompanyShortName { get; set; }
        public string CompanyAddress { get; set; }
        public string CompanyPhone { get; set; }
        public string CompanyEmail { get; set; }
        public string CompanyFax { get; set; }
        public bool RecordState { get; set; }
        public virtual List<Branch> Branches { get; set; }
    }
    


  • NewDBContext类

  • NewDBContext class

    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using projectnetwork.Models.Mapping;
    
    namespace projectnetwork.Models
    {
    public partial class NewDBContext : DbContext
    {
        static NewDBContext()
        {
            Database.SetInitializer<NewDBContext>(null);
        }
    
        public NewDBContext()
            : base("Name=NewDBContext")
        {
            this.Configuration.LazyLoadingEnabled   = false;
            this.Configuration.ProxyCreationEnabled = false;
        }
    
        public DbSet<Branch> Branches { get; set; }
        public DbSet<Company> Companies { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new BranchMap());
            modelBuilder.Configurations.Add(new CompanyMap());          
        }
    }
    }
    


  • 调用方法

  • Call Method

        [WebMethod]
        public Branch getAllCustomers()
        {
            Branch  br = null;
    
            using (var db = new NewDBContext())
            {
                br = db.Branches
                    .Where(d => d.CompanyID == 1)
                    .Include(c => c.Company)
                    .FirstOrDefault<Branch>();                  
    
    
                return br;
    
            }
    


  • 对不起,我的英语不好

    推荐答案

    您可以尝试添加 JsonIgnore 属性如果您使用的是Newtonsoft.Json,则在类上停止渲染它。

    You can try adding a JsonIgnore attribute on your class to stop it being rendered, if you are using Newtonsoft.Json

    public partial class Company
    {
        public Company()
        {
            this.Branches = new List<Branch>();
        }
    
        ....
    
        [JsonIgnore]
        public virtual List<Branch> Branches { get; set; }
    

    }

    这篇关于System.InvalidOperationException:检测到循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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