c# - asp.net core: Preserve ForeignKey Relation in JsonResult -
when send object database (entityframework) using jsonresult without foreign key result need. if object contains foreignkey relation relation not in resulting json:
what get:
{ "agentid":"9990447f-6703-11e7-9c8b-94de80ab7fee", ... "configurationid": 22, "configuration": null } what need:
{ "agentid":"9990447f-6703-11e7-9c8b-94de80ab7fee", ... "configurationid": 22, "configuration": { "id": 0, ... } } what can preserve foreign key relation in json?
i tried set following:
services.addmvc().addjsonoptions(options => options.serializersettings.preservereferenceshandling = preservereferenceshandling.all); ef context:
public class agent { [key] public guid agentid { get; set; } ... public int? configurationid { get; set; } [foreignkey("configurationid")] public configuration configuration { get; set; } } public class configuration { public int id { get; set; } ... public icollection<agent> agents { get; set; } } controller:
[httpget] public jsonresult index() { var agentlist = _databasecontext.agents; return new jsonresult(agentlist); }
you can load related data
[httpget] public jsonresult index() { var agentlist = _databasecontext.agents .include(agent=> agent.configuration) .tolist();; return new jsonresult(agentlist); }
Comments
Post a Comment