title: Entity Framework, Code First modeling and a cyclical reference
body: <p>I've spend several days now, trying to solve this problem. While making a simple project to exemplify my problem, I stumbled upon a possible solution. So, this is sort of a double question. </p>
  
  <p>But first, a little background info:</p>
  
  <p>I just started using Entity Framework 4.1 (EF) and Code First to create the models for my ASP.NET MVC project. I need some models similar to this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace TestApp.Models
  {
      public class Family
      {
          public int ID { get; set; }
          public string Name { get; set; }
  
          public virtual ICollection&lt;Father&gt; Fathers { get; set; }
          public virtual ICollection&lt;Mother&gt; Mothers { get; set; }
      }
  
      public class Mother
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int FamilyID { get; set; }
  
          public virtual ICollection&lt;Child&gt; Children { get; set; }
          public virtual Family Family { get; set; }
      }
  
      public class Father
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int FamilyID { get; set; }
  
          public virtual ICollection&lt;Child&gt; Children { get; set; }
          public virtual Family Family { get; set; }
      }
  
      public class Child
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int MotherID { get; set; }
          public int FatherID { get; set; }
  
          public virtual Mother Mother { get; set; }
          public virtual Father Father { get; set; }
      }
  }
  </code></pre>
  
  <p>And the DbContext:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Entity;
  
  namespace TestApp.Models
  {
      public class TestContext : DbContext
      {
          public DbSet&lt;Family&gt; Families { get; set; }
          public DbSet&lt;Mother&gt; Mothers { get; set; }
          public DbSet&lt;Father&gt; Fathers { get; set; }
          public DbSet&lt;Child&gt; Children { get; set; }
      }
  }
  </code></pre>
  
  <p>(Please excuse the lame example, that's what my Friday fried brain was able to come up with.)</p>
  
  <p>A family can have several mothers and several fathers. And a child has a mother and a father. I checked with one of the .NET gurus at my work, who agreed that there is nothing extraordinary in this. At least as far as we can see.</p>
  
  <p>But when I run the code, I get this Exception:</p>
  
  <blockquote>
    <p>System.Data.SqlServerCe.SqlCeException: The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = Mother_Family ]</p>
  </blockquote>
  
  <p>I do see the cycle: <code>Family - Mother - Child - Father - Family</code>. But if I created the database tables myself (which I prefer not to, that's what I like about Code First) it would be a perfectly valid data structure, as far as I can tell.</p>
  
  <p><strong>So, my first question is:</strong> Why is this a problem when using code first? Is there a way to tell EF how to properly handle the cycle?</p>
  
  <p>Then, as I write initially, while creating a simple project to exemplify my problem, I incidentally stumbled upon a possible solution. I simply forgot some of the properties when defining my models. For clarity in the following example, instead of removing them, I've commented out the parts of the models I forgot:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace TestApp.Models
  {
      public class Family
      {
          public int ID { get; set; }
          public string Name { get; set; }
  
          public virtual ICollection&lt;Father&gt; Fathers { get; set; }
          public virtual ICollection&lt;Mother&gt; Mothers { get; set; }
      }
  
      public class Mother
      {
          public int ID { get; set; }
          public string Name { get; set; }
          // public int FamilyID { get; set; }
  
          public virtual ICollection&lt;Child&gt; Children { get; set; }
          public virtual Family Family { get; set; }
      }
  
      public class Father
      {
          public int ID { get; set; }
          public string Name { get; set; }
          // public int FamilyID { get; set; }
  
          public virtual ICollection&lt;Child&gt; Children { get; set; }
          public virtual Family Family { get; set; }
      }
  
      public class Child
      {
          public int ID { get; set; }
          public string Name { get; set; }
          // public int MotherID { get; set; }
          // public int FatherID { get; set; }
  
          public virtual Mother Mother { get; set; }
          public virtual Father Father { get; set; }
      }
  }
  </code></pre>
  
  <p>So, removing these <code>SomethingID</code> reference properties seems to solve my problem. As you can see in the controller of the sample project I'm linking to in the end of this post, I'm still able to cycle all the way around and do stuff like <code>mothers.First().Family.Fathers.First().Children.First().Mother.Family.Name</code> without any problems. But all tutorials and examples about EF and Code First modeling I've been looking at (e.g. <a href="http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx" rel="noreferrer">this one by Scott Guthrie</a>) include these properties, so it feels wrong not to use them.</p>
  
  <p><strong>And so, my second question is:</strong> Will there be any drawbacks and problems I haven't discovered yet doing this?</p>
  
  <p>Download example project here: <a href="http://blackfin.cannedtuna.org/cyclical-reference-test-app.zip" rel="noreferrer">http://blackfin.cannedtuna.org/cyclical-reference-test-app.zip</a>, and open TestSolution.sln. The properties are commented out in the example project. Uncomment the lines in TestModels.cs to add the properties, resulting in the cyclical reference exception.</p>
  
  <p><strong>NB:</strong> The solution is creating and seeding a SQL CE database located at c:\TestApp.sdf</p>
  
  <p><strong>Update, December 2011:</strong>
  I never solved this problem technically, but I quit my job and found another job where I don't have to use Microsoft technologies. That sort of solved <em>my</em> problem :)</p>
  
  <p>As the tech support at the old place used to write when fixing issues: "A workaround or solution has been provided".</p>