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.