1

I have this many to many association between KundeInfo and HovedKategori, which I have mapped in my MS SQL database like this: enter image description here

I have implemented the methods KundeInfo.HovedKategoris:

public IEnumerable<KundeInfo> KundeInfos
{
    get
    {
        using (var dc = new DataClassesBSMAKSDataContext())
        {
            dc.DeferredLoadingEnabled = false;
            var kundeInfoHovedKategoris = dc.KundeInfoHovedKategoris.Where(x => x.HovedKategori_Id == Id);
            var kundeInfos = dc.KundeInfos.Where(x => kundeInfoHovedKategoris.Any(y => y.KundeInfo_Id == x.Id));

            return kundeInfos.ToList();
        }
    }
}

... and HovedKategori.KundeInfos:

public IEnumerable<HovedKategori> HovedKategoris
{
    get
    {
        using (var dc = new DataClassesBSMAKSDataContext())
        {
            var kundeInfoHovedKategoris = dc.KundeInfoHovedKategoris.Where(x => x.KundeInfo_Id == Id);
            var hovedKategoris = dc.HovedKategoris.Where(x => kundeInfoHovedKategoris.Any(y => y.HovedKategori_Id == x.Id));

            return hovedKategoris.ToList();
        }
    }
}

This retrieves the associated KundeInfos from a specific HovedKategori and opposite. The problemhowever lies in the serialization. When I call ToList(), or serialize these objects to JSON, linq will try to first follow all references returned by HovedKategori.KundeInfos, if it were that method I were to call first, and then it would for each returned object, try to follow all references returned by KundeInfo.HovedKategoris and so on, until it would cast a stack overflow exception.

If I could somehow prevent linq from following certain properties with an [Ignore] attribute or something, it would work, but I haven't been able to find anything like that.

What can I do in this situation?

tereško
  • 58,060
  • 25
  • 98
  • 150
Rasive
  • 1,143
  • 1
  • 8
  • 20
  • Are you sure you get a SO when you call `ToList()`? What do you use for JSON serialization? – Boris B. Aug 22 '14 at 08:59
  • My understanding is that Linq travels the branches of objects and their properties when ToList is called, as in it is made into a list from a linq resultset. The stack overflow exception gets throws either right when I call ToList, or when I serialize the object with Newtonsoft.Json. I even tell Newtonsoft.Json to not follow references, but it doesn't matter, because it seems to be linq that makes the circular references happen. – Rasive Aug 22 '14 at 09:29
  • possible duplicate of [Circular Reference error when serializing objects in ASP.NET Web API](http://stackoverflow.com/questions/17718153/circular-reference-error-when-serializing-objects-in-asp-net-web-api) – Batavia Aug 22 '14 at 22:19

2 Answers2

1

This is in part a design issue. What you should really ask yourself is if you need navigation properties in every possible direction. For example if you just add a Kategori ID instead of a navigation property you could still query your context (by using the ID) but do you really need to always get all the Kategori's with all underlying data?

also if you make your properties virtual you have lazy loading and will only get the information if you .Include it or explicitly reference it.

Batavia
  • 2,497
  • 14
  • 16
0

Ok - So I solved this problem by just making it into methods on the respective classes, which it should be in the first place, since it retrieved these entities from the database. So yes, partially design issue.

Virtual did not work, I considered using projection and an abstract class, but it would be such a haystack of inheritance, and class casts, that it would not even be worth considering.

public IEnumerable<KundeInfo> KundeInfos()
{

    using (var dc = new DataClassesBSMAKSDataContext())
    {
        var kundeInfoHovedKategoris = dc.KundeInfoHovedKategoris.Where(x => x.HovedKategori_Id == Id);
        var kundeInfos = dc.KundeInfos.Where(x => kundeInfoHovedKategoris.Any(y => y.KundeInfo_Id == x.Id));

        return kundeInfos.ToList();
    }
}
Rasive
  • 1,143
  • 1
  • 8
  • 20