2

I have following entity

People
---------
People_id
report_id
last_name
first_name
----------
Navigation properties 
people1     
people2
people_location

Context Class

 public partial class myPeople : DbContext
    {
        public myPeople()
            : base("name=myPeople")
        {
            base.Configuration.ProxyCreationEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<people_location> project_status { get; set; }
        public DbSet<people> people { get; set; }
    }
}

Model Class generated by EF


 [DataContract(IsReference = false)]
    [Serializable]
    public partial class person
    {
        public people()
        {
            this.people1 = new HashSet<person>();
            this.project_discussion = new HashSet<people_location>();
        }

        public int people_id { get; set; }
        public Nullable<int> report_id { get; set; }
        public string last_name { get; set; }
        public string first_name { get; set; }

        public virtual ICollection<people> people1 { get; set; }
        public virtual people people2 { get; set; }
        public virtual ICollection<people_location> people_location { get; set; }
    }
}

In Web API controller

// GET api/personLoc
 public List<people> Getpeople()
    {
        return db.people.AsEnumerable().ToList();
    }

When I run the API to make sure it is working

../api/personLoc/

I just get bunch of empty records

[
  {},
  {},
  {},
  {}
]

I believe the issue is decoration of

[DataContract(IsReference = false)]
    [Serializable]

When I remove this from class I get following exception

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.</ExceptionMessage>System.InvalidOperationException</ExceptionType>An error has occurred.</Message>Object graph for type 'CMEApp.Entities.person' contains cycles and cannot be serialized if reference tracking is disabled.</ExceptionMessage>

Please let me know how to fix it.

J. Davidson
  • 3,297
  • 13
  • 54
  • 102

1 Answers1

0

You seem to have a circular dependency. You need to mark IsReference true in your Person DataContract as follows,

 [DataContract(IsReference = true)]
    [Serializable]
    public partial class person

You can get further information about IsRefernce attribute from MSDN. There is a similar stackoverflow thread here

Community
  • 1
  • 1
Tharaka
  • 2,355
  • 1
  • 20
  • 22
  • While this answer may theoretically answer the question, [it is better](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – hichris123 Mar 24 '14 at 23:52