EF 6 Lazy Loading Disabled but Child Record Loads Anyway(EF 6 延迟加载已禁用,但仍会加载子记录)
问题描述
我首先使用的是 EF6 代码.有两个表,Lesson
和 LessonSections
.LessonSections
表有一个指向 Lesson.Id
I'm using EF6 code first. There are two tables, Lesson
and LessonSections
. The LessonSections
table has a foreign key to Lesson.Id
这是 Lesson
类,没有删除任何重要字段:
Here is the Lesson
class with none important fields removed:
public partial class Lesson
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Lesson()
{
LessonSections = new HashSet<LessonSection>();
}
[StringLength(50)]
public string Id { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<LessonSection> LessonSections { get; set; }
}
这是我启动数据模型的方式:
Here is how I'm initiating my data model:
var db = new Touch_Type_Trainer_DB.DataModel();
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
就在我第一次调用数据库以检索数据库中的第一课之后,结果对象没有LessonSections
Just after my first call to the database to retrieve the first lesson in the database, the resulting object has no LessonSections
然后我再次调用以将这些部分检索到一个单独的对象中.(它们必须在一个单独的对象中,因为我想将它们序列化为 JSON 字符串,并且如果我使用标准 EF,序列化程序会在 Lesson
和 LessonSections
之间的循环引用上停止延迟加载.)
Then I make a second call to retrieve the sections into a separate object. (They must be in a separate objects since I want to serialize them to a JSON string and the serializer halts on the circular reference between Lesson
and LessonSections
if I use the standard EF LazyLoading.)
即使我从未访问过 LessonSections
属性并且即使 LazyLoadingEnabled 设置为 False,我的原始对象也从数据库加载了两个部分!
Now my original object has two sections loaded from the database even though I never accessed the LessonSections
property and even though LazyLoadingEnabled is set to False!
为什么会加载 LessonSections
?
我正在使用 Newtonsoft 将我的对象序列化为 JSON 字符串.也许我应该在 Newtonsoft 中设置一个配置设置,以免陷入循环引用问题?
I'm using Newtonsoft to serialize my object into a JSON string. Maybe there is a configuration setting in Newtonsoft that I should be setting so it doesn't get caught in the circular reference problem?
另外,我确实希望为大部分代码启用 LazyLoading,而不是为序列化部分启用.
Also, I do want LazyLoading enabled for the majority of the code, just not for the serializing part.
推荐答案
这是你的问题,还是你只是好奇为什么会这样?
is this a problem for you, or are you just curious as to why its happening?
DBContext 为您跟踪所有引用.当您加载这些部分时,它会知道课程有对它们的引用,并为您连接起来.
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.
您可以通过断开对象或从不同的 dbcontext 加载部分来停止此操作
you could stop this by disconnecting the objects , or by loading the sections from a different dbcontext
myDbContext.Entry(someLesson).State=Detached;
有关序列化问题,请参阅此问答 你怎么真的"使用 Newtonsoft.Json 序列化循环引用对象?
For the serialization issue, see this Q&A How Do You "Really" Serialize Circular Referencing Objects With Newtonsoft.Json?
或
http:///johnnycode.com/2012/04/10/serializing-circular-references-with-json-net-and-entity-framework/
这篇关于EF 6 延迟加载已禁用,但仍会加载子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:EF 6 延迟加载已禁用,但仍会加载子记录
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04