如何正确使用EF4导航属性? [英] How do I correctly use EF4 Navigation Properties?

查看:260
本文介绍了如何正确使用EF4导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用EF4模型 - 第一种方法创建了一个数据库。在我的模型中,两个实体之间有一个N到M的关系:





我已经用一些虚拟数据填充了我的数据库,包括3个类型的记录诊断和3个记录类型 TreatmentSchema 和它们之间的关联。以下是我以前使用的代码片段:

  using(var container = new SmartTherapyContainer()){
var diagnosisA = new Diagnosis(){Id = Guid.NewGuid(),Name =Diagnosis A};
var diagnosticB = new Diagnosis(){Id = Guid.NewGuid(),Name =Diagnosis B};
var diagnosticC = new Diagnosis(){Id = Guid.NewGuid(),Name =Diagnosis C};
container.Diagnoses.AddObject(diagnosticA);
container.Diagnoses.AddObject(diagnosticB);
container.Diagnoses.AddObject(diagnosticC);

var schemaA = new TreatmentSchema(){Id = Guid.NewGuid(),Name =Schema 1};
var schemaB = new TreatmentSchema(){Id = Guid.NewGuid(),Name =Schema 1};
var schemaC = new TreatmentSchema(){Id = Guid.NewGuid(),Name =Schema 1};
container.Schemas.AddObject(diagnosticA);
container.Schemas.AddObject(diagnosticB);
container.Schemas.AddObject(diagnosticC);

diagnosticB.TreatmentSchemas.Add(schemaA);
diagnosticC.TreatmentSchemas.Add(schemaA);
diagnosticC.TreatmentSchemas.Add(schemaB);
diagnosticC.TreatmentSchemas.Add(schemaC);

container.SaveChanges();
}

我验证了关联确实存储在通过EF4的映射创建的参考表中。但是,当我通过 container.Diagnoses 收集诊断之后,其 .TreatmentSchemas 集合始终为空。



我尝试调试到EF4生成的代码,它所做的只是懒惰地创建所述集合, t用相关对象填充它。 Ayende的实体框架分析器显示,当访问该属性时,根本就不会生成查询,这导致我相信我在这里做错了。



如何获取相关的 TreatmentSchemas 的列表?

解决方案

p>导航属性默认不加载。您必须使用加载加载或惰性加载,但是由于您使用自我跟踪实体,您的选择只是迫切加载,因为 STE不支持延迟加载。所以如果你想得到所有相关的治疗方案的Diagonstic实例,你必须调用:

  var diagnostic = context.Diagnoses.Include( TreatmentSchemas)FirstOrDefault(); 


I've created a database using the EF4 model-first approach. In my model, there's an N-to-M relationship between two entities:

I've filled my database with some dummy data, including 3 records of type Diagnosis and 3 records of type TreatmentSchema and associations between them. Here's the code snippet I used to do this:

using(var container = new SmartTherapyContainer()) {
  var diagnosisA = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis A" };
  var diagnosisB = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis B" };
  var diagnosisC = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis C" };
  container.Diagnoses.AddObject(diagnosisA);
  container.Diagnoses.AddObject(diagnosisB);
  container.Diagnoses.AddObject(diagnosisC);

  var schemaA = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
  var schemaB = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
  var schemaC = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
  container.Schemas.AddObject(diagnosisA);
  container.Schemas.AddObject(diagnosisB);
  container.Schemas.AddObject(diagnosisC);

  diagnosisB.TreatmentSchemas.Add(schemaA);
  diagnosisC.TreatmentSchemas.Add(schemaA);
  diagnosisC.TreatmentSchemas.Add(schemaB);
  diagnosisC.TreatmentSchemas.Add(schemaC);

  container.SaveChanges();
}

I verified that the associations are indeed stored in the reference table created through EF4's mapping. However, when I retrieve a Diagnosis via the container.Diagnoses collection later, its .TreatmentSchemas collection is always empty.

I tried debugging into the EF4-generated code and all it does is lazily create said collection, but it doesn't fill it with the associated objects. Ayende's Entity Framework Profiler shows no queries being generated at all when the property is accessed, which leads me to believe that I'm doing something wrong here.

How can I get a list of the associated TreatmentSchemas?

解决方案

Navigation properties are not loaded by default. You must use either eager loading or lazy loading but because you are using self tracking entities your choice is only eager loading because STEs don't support lazy loading. So if you want to get Diagonstic instance with all related TreatmentSchemas you must call:

var diagnosis = context.Diagnoses.Include("TreatmentSchemas").FirstOrDefault();

这篇关于如何正确使用EF4导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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