I have a below model from the EF entity:
public partial class Commodity
{
public Commodity()
{
this.CommodityVarieties = new HashSet<CommodityVariety>();
}
public int CommodityID { get; set; }
public string CommodityName { get; set; }
public string CommodityVarietyDisplayName { get; set; }
public string BackColor { get; set; }
public string ForeColor { get; set; }
public bool IsDeleted { get; set; }
public int SortOrder { get; set; }
public virtual ICollection<CommodityVariety> CommodityVarieties { get; set; }
}
public partial class CommodityVariety
{
public int VarietyID { get; set; }
public int CommodityID { get; set; }
public string VarietyName { get; set; }
public bool IsDeleted { get; set; }
public virtual Commodity Commodity { get; set; }
}
I would like to get a list of commodities and convert that list to JSON string using Newtonsoft
. Hence, I write
DbContext context = new DbContext();
var list = context.Commodities.ToList();
string json = JsonConvert.SerializeObject(list);
I am getting below error:
Self referencing loop detected for property 'Commodity' with type 'System.Data.Entity.DynamicProxies.Commodity_B55D25F995ED72E0B75FED715153713965D91EB5A3BF576322FE6DEAC130C0F5'. Path '[0].CommodityVariety[0]
I know it is because of the reference to Commodity
in the CommodityVariety
class.
To avoid I updated JSON Serializing Setting to have ReferenceLoopHandling
to Ignore
like below:
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Above settings give me
Exception of type 'System.OutOfMemoryException' was thrown.
I have tried all the possible answers from the StackOverflow.
Finally, I did
public DbContext() : base("name=DbContextEntity")
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
Now, I get the code running without any error and Commodity.CommodityVarieties
is null.
I have so many foreign keys and it is really hard to map them all manually after setting ProxyCreationEnabled
to false
.
Is there any way to identify self-referencing before JSON serializing and make it null? Like below:
DbContext context = new DbContext();
var list = context.Commodities.ToList();
//filter only properties of `Commodity` property and find object of type `Commodity`
//var suspectObjects = list.Any(x => x.OfType<Commodity>()).ToList();
//suspectObjects.ForEach(item => { item = null; });
//I know the above segment will not work. I seek your help and I thought something like above explain more to you what I want actually.
string json = JsonConvert.SerializeObject(list);