Entity Framework 4.0 自跟踪加载导航属性

发布于 2024-11-25 20:17:22 字数 590 浏览 2 评论 0原文

我的数据访问类有这段代码,我想在其中加载所有患者应有的位置,Patient.Addresses、Patient.Examinations、Examinations.LeftEye、Examinations.RightEye ....

如何加载所有相关数据一个病人?我连地址都没有加载。

这是当前的代码:

            db.Patients.Include("Addresses");
            db.Patients.Include("Examinations");

            db.Examinations.Include("LeftEyePictures");
            db.Examinations.Include("RightEyePictures");

            List<Patient> list = db.Patients.ToList();

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

            return list;

谢谢。

I have this code for my Data Access class where I want to load all the Patients where they some should have, Patient.Addresses, Patient.Examinations, Examinations.LeftEye, Examinations.RightEye....

How to Load all the related data with a patient? I havent managed to load even the address.

This is the current code:

            db.Patients.Include("Addresses");
            db.Patients.Include("Examinations");

            db.Examinations.Include("LeftEyePictures");
            db.Examinations.Include("RightEyePictures");

            List<Patient> list = db.Patients.ToList();

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

            return list;

Thank you.

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

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

发布评论

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

评论(2

随遇而安 2024-12-02 20:17:23

有两种方法:
第一:
加载前启用延迟加载

db.ContextOptions.LazyLoadingEnabled = true;
List<Patient> list = db.Patients.ToList(); // here is All Patients with Linked objects
db.ContextOptions.LazyLoadingEnabled = false;

然后禁用,如果您不需要它

第二:手动加载

   db.Patients.Addresses.Load()
   db.Patients.Examinations.Load()
   db.Examinations.LeftEyePictures.Load()
   db.Examinations.RightEyePicturesLoad()

您可以在加载前检查:(此或类似)

   if(db.Patients.Addresses.IsLoaded==false)
      db.Patients.Addresses.Load

there is two ways:
1st:
enable lazzy loading before loading

db.ContextOptions.LazyLoadingEnabled = true;
List<Patient> list = db.Patients.ToList(); // here is All Patients with Linked objects
db.ContextOptions.LazyLoadingEnabled = false;

Then disable, if you dont need it

2nd:Handload

   db.Patients.Addresses.Load()
   db.Patients.Examinations.Load()
   db.Examinations.LeftEyePictures.Load()
   db.Examinations.RightEyePicturesLoad()

you can chesk before loading: (this or similar)

   if(db.Patients.Addresses.IsLoaded==false)
      db.Patients.Addresses.Load
半窗疏影 2024-12-02 20:17:22

Include 必须是查询的一部分!

List<Patient> list = db.Patients.Include("Addresses")
                                .Include("Examinations.LeftEyePicutres")
                                .Include("Examinations.RightEyePicutres")
                                .ToList();

Include must be part of the query!

List<Patient> list = db.Patients.Include("Addresses")
                                .Include("Examinations.LeftEyePicutres")
                                .Include("Examinations.RightEyePicutres")
                                .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文