System.InvalidOperationException:检测到循环引用

时间:2014-03-10 07:16:27

标签: c# entity-framework

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的对象,即子公司包含其父公司的对象,即公司。

  • 分类

    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; }        
        }
    
  • 公司类

    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类

    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());          
        }
    }
    }
    
  • 通话方法

        [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;
    
            }
    

抱歉我的英文不好

2 个答案:

答案 0 :(得分:1)

如果您使用的是Newtonsoft.Json,可以尝试在类上添加JsonIgnore属性以停止呈现

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

    ....

    [JsonIgnore]
    public virtual List<Branch> Branches { get; set; }

}

答案 1 :(得分:0)

您似乎需要将DataContract属性的IsReference属性设置为true。一些额外的细节是here

[DataContract(IsReference = true)]