0

I have a little SQL Server 2012 "School" DB used for testing. It has these TableName(FieldName1, FieldName2....):

Course(CourseNo, CourseName), Student(StudentId, StudentName, Birth), Enrollment(EnrollNo, StudentNo, CourseId)

The Enrollment table uses foreign keys to link a Student to Courses - CourseNo:CourseID and StudentId:StudentNo

When an EF Model-diagram is created in a VS2013 web site, the Enrollment entity shows "navigation" to Course and Student

I've use JSON in an html page to access a "Controller" that uses EF6 and linq to retrieve data from the DB to populate a page in a jQuery Mobile app. The first test is to just list the contents of the "Course" table in an html table. If my EF Model just contains the Course table, the code works perfectly. If I add the Student and Enrollment tables (and do nothing else) the code fails with debugger info saying that JSON couldn't resolve naming issues. All the field names are unique but the Enrollment table navigation shows Course and Student. Apparently, any reference involving the same names causes JSON to fail. As a side test, adding an ASP page to the site with a Gridview, that uses the exact same DB retrieval code, with all three tables present, works perfectly.

Is there something I'm missing when trying to combine JSON and EF6-linq?

Here is the important parts of the html side:

  $.getJSON("api/CourseTitles",
                           function (data) {
                               $('#coursenames').empty(); // Clear the table body.
                               // Loop through the list of courses.
                               $.each(data, function (key, val) {
                                   // Add a table row for the course.
                                   $('#coursenames').append('<tr><td>' + val.CourseNo + '&nbsp;&nbsp;&nbsp;' +
                                           '</td><td>' + val.CourseName + '</td></tr>');
                               });
                           });
 .................
                            <table>
                                <thead>
                                    <tr><th>Number</th><th>Name</th></tr>
                                </thead>
                                <tbody id="coursenames"></tbody>
                            </table>

Here is the Controller code:

    Public Function GetCourseTitles() As IEnumerable(Of Course)
        Dim myContext As New SchoolEntities1()
        Return From courselist In myContext.Courses Select courselist
    End Function

Edit - This works:

       Public Function GetCourseTitles() As IEnumerable(Of Course)
        Dim myContext As New SchoolEntities()
        myContext.Configuration.ProxyCreationEnabled = False
        Return From courselist In myContext.Courses Select courselist
      End Function
Bicycle Dave
  • 484
  • 1
  • 7
  • 25

1 Answers1

0

Are you eager or lazy loading the related entities? If you have EF set up to lazy load the related entities then your controller code will only return the course data and not any of the related data by default. You can use the Include method to eager load the related entities prior to returning the data to the page.

  • Hi Todd, Thanks for the suggestion and I can look at this. However, I just resolved the problem from another post http://stackoverflow.com/questions/16949520/circular-reference-detected-exception-while-serializing-object-to-json. I added this and the problem disappeared: myContext.Configuration.ProxyCreationEnabled = False – Bicycle Dave Feb 12 '14 at 22:45
  • No problem. That solution should work in this situation because you aren't worried about tracking changes. – Todd Miranda Feb 12 '14 at 22:52
  • I'm not sure I fully understand this fix, but it sounds like maintaining the state of an EF object is not useful in a website app and nothing is lost by disabling this proxy. Still seems odd that it fails with the proxy enabled. But, so be it. – Bicycle Dave Feb 12 '14 at 23:00
  • Tracking is really only used by EF to determine what has changed since the data was retrieved. Thus it is not necessary when reading and displaying data, just when updating. The proxy that is used to wrap your entities is not serializable, which is probably what was causing your initial issue. The proxy is also responsible for the ability to lazy load navigation entities. So with it off, you need to make sure you are eager loading those entities if you want to display them as well. Hope that helps a bit. – Todd Miranda Feb 21 '14 at 14:55