如何加载实体框架 Self Track TrackableCollection仅在需要时才提供关系数据?

发布于 2024-11-30 05:30:13 字数 2062 浏览 7 评论 0原文

我使用 Entity Framework 4 和自我跟踪实体。模式如下:

Patient ->考试->左图 -> RightPictures

因此存在这两个关系 Patient 1 - * ....Pictures 的 TrackableCollection。 现在,当加载客户表单并浏览详细信息时,我不需要加载这些 数据图像,仅当加载另一个表格以获取考试详细信息时!

我使用类库作为数据存储库从数据库(SQL Server)获取数据,代码如下:

public List<Patient> GetAllPatients()
{
    try
    {
        using (OptoEntities db = new OptoEntities())
        {
            List<Patient> list = db.Patients
                .Include("Addresses")
                .Include("PhoneNumbers")
                .Include("Examinations").ToList();

            list.ForEach(p =>
                             {
                                 p.ChangeTracker.ChangeTrackingEnabled = true;

                                 if (!p.Addresses.IsNull() &&
                                     p.Addresses.Count > 0)
                                     p.Addresses.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);

                                 if (!p.PhoneNumbers.IsNull() &&
                                     p.PhoneNumbers.Count > 0)
                                     p.PhoneNumbers.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);

                                 if (!p.Examinations.IsNull() &&
                                     p.Examinations.Count > 0)
                                     p.Examinations.ForEach(e =>
                                                                {
                                                                    e.ChangeTracker.ChangeTrackingEnabled = true;

                                                                });
                             });

            return list;
        }
    }
    catch (Exception ex)
    {
        return new List<Patient>();
    }
}

现在我需要在调用考试详细信息表单时获取考试关系的所有图像(LeftEyePictures、RightEyePictures)。我想这就是所谓的延迟加载,当我立即关闭实体连接时,我不明白如何实现它,我想保持这样。

我通过应用程序使用 BindingSource 组件。

获得所需结果的最佳方法是什么?

谢谢。

I use Entity Framework 4 and Self Tracking Entities. The schema is like:

Patient -> Examinations -> LeftPictures
-> RightPictures

So there is TrackableCollection of these two relationships Patient 1 - * ....Pictures.
Now when loading the customers Form and browsing the details I dont need to load these
data images, only when another form is loaded for Examination details!

I am using a class library as a Data Repository to get data from the database (SQL Server) and this code:

public List<Patient> GetAllPatients()
{
    try
    {
        using (OptoEntities db = new OptoEntities())
        {
            List<Patient> list = db.Patients
                .Include("Addresses")
                .Include("PhoneNumbers")
                .Include("Examinations").ToList();

            list.ForEach(p =>
                             {
                                 p.ChangeTracker.ChangeTrackingEnabled = true;

                                 if (!p.Addresses.IsNull() &&
                                     p.Addresses.Count > 0)
                                     p.Addresses.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);

                                 if (!p.PhoneNumbers.IsNull() &&
                                     p.PhoneNumbers.Count > 0)
                                     p.PhoneNumbers.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);

                                 if (!p.Examinations.IsNull() &&
                                     p.Examinations.Count > 0)
                                     p.Examinations.ForEach(e =>
                                                                {
                                                                    e.ChangeTracker.ChangeTrackingEnabled = true;

                                                                });
                             });

            return list;
        }
    }
    catch (Exception ex)
    {
        return new List<Patient>();
    }
}

Now I need when calling the Examination details form to go and get all the Images for the Examination relationship (LeftEyePictures, RightEyePictures). I guess that is called Lazy Loading and I dont understood how to make it happen while I'm closing the Entities connection immidiately and I would like to stay like this.

I use BindingSource components through the application.

What is the best method to get the desired results?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-12-07 05:30:13

自跟踪实体不支持延迟加载。此外,延迟加载仅在实体附加到实时上下文时才起作用。您不需要立即关闭/处置上下文。对于 WinForms 应用程序上下文,通常会存在较长时间(您可以遵循每个表单一个上下文或每个演示者一个上下文的方法)。

WinForms 应用程序是普通附加实体的场景,其中所有这些功能(例如延迟加载或更改跟踪)都可以开箱即用。 STE 应该用在分布式系统中,您需要序列化实体并将其传递给另一个应用程序(通过 Web 服务调用)。

Self tracking entities don't support lazy loading. Moreover lazy loading works only when entities are attached to live context. You don't need to close / dispose context immediately. In case of WinForms application context usually lives for longer time (you can follow one context per form or one context per presenter approach).

WinForms application is scenario for normal attached entities where all these features like lazy loading or change tracking work out of the box. STEs are supposed to be used in distributed systems where you need to serialize entity and pass it to another application (via web service call).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文