Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple relations between two tables
    primarykey
    data
    text
    <p>I have two classes I want to store in EF Code First. A Building has a maintainer, and a list of people working there. The mainainer doesn't have to work in the building.</p> <p>At my first attempt I just had</p> <pre><code>public class Person { public virtual Guid Id { get; set; } public virtual string Name { get; set; } } </code></pre> <p>and </p> <pre><code>public class Building { public virtual Guid Id { get; set; } public virtual List&lt;Person&gt; WorksHere { get; set; } public virtual Person MaintainedBy { get; set; } public virtual String Address { get; set; } } </code></pre> <p>This gives me two tables with the base properties, and a Building_Id on People, and a MaintainedBy_Id on Buildings.</p> <p>When I run a test program</p> <pre><code> using (TestContext tc = new TestContext()) { Person m1 = tc.persons.Create(); m1.Name = "maintainB1"; m1.Id = Guid.NewGuid(); Person m2 = tc.persons.Create(); m2.Name = "maintainB2"; m2.Id = Guid.NewGuid(); Building b1 = tc.buildings.Create(); b1.Address = "building1"; b1.Id = Guid.NewGuid(); tc.buildings.Add(b1); Building b2 = tc.buildings.Create(); b2.Address = "building1"; b2.Id = Guid.NewGuid(); tc.buildings.Add(b2); b1.MaintainedBy = m1; b2.MaintainedBy = m2; if (b1.WorksHere == null) b1.WorksHere = new List&lt;Person&gt;(); if (b2.WorksHere == null) b2.WorksHere = new List&lt;Person&gt;(); b1.WorksHere.AddRange(new List&lt;String&gt;() { "e11", "e12", "e13" }.Select(s =&gt; { Person p = new Person(); p.Id = Guid.NewGuid(); p.Name = s; return p; })); b2.WorksHere.AddRange(new List&lt;String&gt;() { "e21", "e22", "e23" }.Select(s =&gt; { Person p = new Person(); p.Id = Guid.NewGuid(); p.Name = s; return p; })); b1.WorksHere.Add(m2); b2.WorksHere.Add(m1); tc.SaveChanges(); } } </code></pre> <p>I get an exception: "An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details."</p> <p>with innerexception: "Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values."</p> <p>I would prefer not to expose any more properties on my Poco's, because, well, they are Poco's, and I have no use for those properties. If I have to to satisfy Code First model generation, than that'll have to do, but if at all possible, I'd like to have that away from my Poco's in a mapping class.</p> <p>How do I fix this?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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