EF 6 Lazy Loading Disabled but Child Record Loads Anyway

后端 未结 2 2010
日久生厌
日久生厌 2021-01-14 00:33

I\'m using EF6 code first. There are two tables, Lesson and LessonSections. The LessonSections table has a foreign key to Lesson

2条回答
  •  盖世英雄少女心
    2021-01-14 00:44

    is this a problem for you, or are you just curious as to why its happening?

    The DBContext keeps track of all references for you. When you load the sections, it knows that the lessons have references to them, and wires it up for you.

    you could stop this by disconnecting the objects , or by loading the sections from a different dbcontext

    myDbContext.Entry(someLesson).State=Detached;
    

    For the serialization issue, see this Q&A How Do You "Really" Serialize Circular Referencing Objects With Newtonsoft.Json?

    or

    http://johnnycode.com/2012/04/10/serializing-circular-references-with-json-net-and-entity-framework/

提交回复
热议问题