0

I have an database with foreign keys and a model loaded from it. There are circular references in the objects due to these inherited references.

In my case, Course and SiteUser are object references. When I run the code below, the references do not break. Is there a proper/easy way to break these circular references? My research has shown me datacontracts but our database is massive and would require a lot of work.

public partial class ScormActivity
{
    public static ScormActivity filterCircularReferences(ScormActivity activity)
    {
        activity.Course = null;
        activity.SiteUser = null;
        return activity;
    }
}
Zakk Diaz
  • 1,063
  • 10
  • 15
  • What library are you using to convert to JSON? – Rob Mar 18 '16 at 00:56
  • System.Web.MVC.Controller.Json – Zakk Diaz Mar 18 '16 at 00:57
  • Take a look here: http://stackoverflow.com/questions/2002940/json-and-circular-reference-exception – Rob Mar 18 '16 at 00:59
  • Maybe this is a lack of my knowledge of c#, but to my understanding I need to make a derived class to override the member to add the ScriptIgnore attribute. Then I have to memberwise assign the original class to the derived class. At this point I'd rather return a new instance of the class without assigning Course and SiteUser members. – Zakk Diaz Mar 18 '16 at 01:18
  • Are you unable to put the attributes on the original class? Also, I *believe* `JSON.Net` has support for circular references - so you might be able to use that rather than the MVC `Json()`. But yes, circular references with JSON is a pain in general, really. I'd avoid it as much as possible – Rob Mar 18 '16 at 02:33
  • No, the original class is generated so any modifications would be wiped out upon regeneration. I got past it for now by returning a new instance of the class and copying the members I need for the specific use. I could pick up develop speed if I knew how to do this. I'll check out JSON.Net when I need it again, thank you. – Zakk Diaz Mar 19 '16 at 01:38

0 Answers0