Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I correctly use EF4 Navigation Properties?
    primarykey
    data
    text
    <p>I've created a database using the EF4 model-first approach. In my model, there's an N-to-M relationship between two entities:</p> <p><img src="https://i.stack.imgur.com/YKqGv.png" alt="enter image description here"></p> <p>I've filled my database with some dummy data, including 3 records of type <code>Diagnosis</code> and 3 records of type <code>TreatmentSchema</code> and associations between them. Here's the code snippet I used to do this:</p> <pre><code>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(); } </code></pre> <p>I verified that the associations are indeed stored in the reference table created through EF4's mapping. However, when I retrieve a <code>Diagnosis</code> via the <code>container.Diagnoses</code> collection later, its <code>.TreatmentSchemas</code> collection is always empty.</p> <p>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 <em>Entity Framework Profiler</em> shows no queries being generated at all when the property is accessed, which leads me to believe that I'm doing something wrong here.</p> <p>How can I get a list of the associated <code>TreatmentSchemas</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload