title: Entity Framework with Condition Relationship
body: <p>Is it possible to have a relationship that is based on a condition in Entity Framework? My model looks something like this...</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Document
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public OwnerType OwnerType { get; set; }
      public int OwnerId { get; set; }
  
      public virtual Organization OrganizationOwner { get; set; }
      public virtual User UserOwner { get; set; }
  }
  
  public enum OwnerType
  {
      Organization = 1,
      User = 2
  }
  
  public class Organization
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      //[other properties specific to Organization]
  
      public virtual List&lt;Documents&gt; Documents { get; set; }
  }
  
  public class User
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      //[other properties specific to User]
  
  
      public virtual List&lt;Documents&gt; Documents { get; set; }
  }
  </code></pre>
  
  <p>So, what I'd like to is set up a relationship so that the OrganizationOwner property of a Document instance automatically gets populated when the OwnerType == OwnerType.Organization, and the UserOwner property is populated when OwnerType == OwnerType.User.</p>
  
  <p>Is this possible to set up this kind of relationship in EntityFramework - Code First? Something like this in the mapping...</p>
  
  <pre class="lang-cs prettyprint-override"><code>EntityTypeConfiguration&lt;Document&gt;.HasOptional(d =&gt; d.OrganizationOwner)
      .WithMany(o =&gt; o.Documents)
      .HasForeignKey(d =&gt; d.OwnerId)
      .Where(d =&gt; d.OwnerType == OwnerType.Organization);
  
  EntityTypeConfiguration&lt;Document&gt;.HasOptional(d =&gt; d.UserOwner)
      .WithMany(u =&gt; u.Documents)
      .HasForeignKey(d =&gt; d.OwnerId)
      .Where(d =&gt; d.OwnerType == OwnerType.User);
  </code></pre>
  
  <p>I would like to be able to leverage joins on the OrganizationOwner and UserOwner when setting up my Linq queries on the context so that I don't have to do a separate selects on those entities for each Document. Is this type of relationship supported or is there a better way to do this? Thanks.</p>