I'm lazy loading my table data to a JSON result and wending them to front end application. but when I get those data I notice that there are unnecessary elements, empty elements present in that response. And because of that my PUT
or update operation do not work for these inner JSON properties.
{
"image":null,
"paragraph":null,
"question":{
"grid":null,
"elements":[
],
"offeredAnswers":[
],
"lazyLoader":{
},
"id":"1",
"text":"How can we serve you better?",
"type":"textarea",
"questionRequired":false,
"pageFlowModifier":false,
"gridId":null,
"min":null,
"max":null
},
"lazyLoader":{
}
}
If I change the value of text
, it will not get updated, but if i change paragraph
then it will be changed in the database.
Here there's a new property called lazyLoader
, I need to get rid of that too. and elements
, offeredAnswers
are really not needed since they are empty. I achived lazy loading by adding virtual
keyword to referenced classes.
public partial class Questions
{
public Questions()
{
Elements = new HashSet<Elements>();
OfferedAnswers = new HashSet<OfferedAnswers>();
}
public string Id { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public bool QuestionRequired { get; set; }
public bool PageFlowModifier { get; set; }
public int? GridId { get; set; }
public long? Min { get; set; }
public long? Max { get; set; }
public virtual Grids Grid { get; set; }
public virtual ICollection<Elements> Elements { get; set; }
public virtual ICollection<OfferedAnswers> OfferedAnswers { get; set; }
}
And I have this line in the Startup.cs
file to stop reference loop handling because Without that the POST
operation do not work since the JSON
object I'm posting is quite complex and has reference loops inside of it.
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
And I have enabled Lazy Loading Proxies.
services.AddDbContext<RDS_Context>
(options => options.UseLazyLoadingProxies().UseSqlServer(connection));