EF 6 延迟加载已禁用,但仍会加载子记录 [英] EF 6 Lazy Loading Disabled but Child Record Loads Anyway

查看:15
本文介绍了EF 6 延迟加载已禁用,但仍会加载子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用的是 EF6 代码.有两个表,LessonLessonSections.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,序列化程序会在 LessonLessonSections 之间的循环引用上停止延迟加载.)

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 延迟加载已禁用,但仍会加载子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆