c# - Empty data being returned by entity using webapi -
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 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
// api/personloc public list<people> getpeople() { return db.people.asenumerable().tolist(); }
when run api make sure working
../api/personloc/
i bunch of empty records
[ {}, {}, {}, {} ]
i believe issue decoration of
[datacontract(isreference = false)] [serializable]
when remove class following exception
the 'objectcontent`1' type failed serialize response body content type 'application/json; charset=utf-8'.</exceptionmessage>system.invalidoperationexception</exceptiontype>an error has occurred.</message>object graph type 'cmeapp.entities.person' contains cycles , cannot serialized if reference tracking disabled.</exceptionmessage>
please let me know how fix it.
you seem have circular dependency. need mark isreference true in person datacontract follows,
[datacontract(isreference = true)] [serializable] public partial class person
you can further information isrefernce attribute msdn. there similar stackoverflow thread here
Comments
Post a Comment