title: Entity Framework include property
body: <p>Originally, I had a basic <code>Client</code> consisting of just:</p>
  
  <pre><code>public class Client : Entity
  {
      [Required]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>My <code>User</code>s, along with the usual <code>Name</code> fields, etc. have a <code>Client</code>:</p>
  
  <pre><code>public class User : Entity
  {
      [Required, MinLength(4), Display(Name = "Username")]
      public string Username { get; set; }
      [Required, MinLength(2), Display(Name = "First Name")]
      public string FirstName { get; set; }
      [Required, MinLength(2), Display(Name = "Last Name")]
      public string LastName { get; set; }
      [Required, EmailAddress]
      public string Email { get; set; }
      ...
      [Required]
      public virtual Client Client { get; set; }
  }
  </code></pre>
  
  <p>My <code>Get</code> method:</p>
  
  <pre><code>public virtual IEnumerable&lt;TEntity&gt; Get(
          Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null,
          Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null,
          string[] includeProperties = null)
      {
          IQueryable&lt;TEntity&gt; query = dbSet;
  
          if (includeProperties != null)
              query = includeProperties.Aggregate(query, (q, s) =&gt; q.Include(s));
  
          query = query.Where(x =&gt; !x.Deleted);
  
          if (filter != null)
              query = query.Where(filter);
  
          return orderBy != null ? orderBy(query).ToList() : query.ToList();
      }
  </code></pre>
  
  <p>All this was fine. So, on to the problem: It's actually with my Client, since adding an additional property for an <code>AccountManager</code> (i.e. a <code>User</code>), it doesn't like me.</p>
  
  <pre><code>public class Client : Entity
  {
      [Required]
      public string Name { get; set; }
  
      [Required]
      public CompanyType CompanyType { get; set; }
  
      [Required]
      public ClientStatus Status { get; set; }
  
      [SomethingInHere?]
      public virtual User AccountManager { get; set; }
  
      public DateTime? AccessFrom { get; set; }
      public DateTime? AccessTo { get; set; }
  
      public DateTime? ClosedDate { get; set; }
  }
  </code></pre>
  
  <p>I've added "AccountManager" to the <code>INCLUDE_STRING</code> for the <code>Get</code>s and even recently tried using various <code>ForeignKey</code>/<code>Required</code> data annotations but can't get it to pull out the AccountManager <code>User</code> along with it.</p>
  
  <p>And with that, I assume it's part of, if not the whole reason that I can no longer pull out any <code>User</code>s at all.</p>
  
  <p>In short, my grasp of EF's relationship handling is somewhat limited, and it would appear that my Googling abilities today aren't up to scratch.</p>
  
  <p>Many <code>User</code>s to a <code>Client</code>. One <code>User</code> as <code>AccountManager</code> for each <code>Client</code>. Why is this so hard for me?</p>
  
  <p>Edit: Rounded answer.</p>
  
  <p>Added in modelbuilder parts:</p>
  
  <pre><code>modelBuilder.Entity&lt;User&gt;()
              .HasRequired(x =&gt; x.Client)
              .WithMany(x =&gt; x.Users);
  
  modelBuilder.Entity&lt;Client&gt;()
              .HasOptional(x =&gt; x.AccountManager);
  </code></pre>
  
  <p>and altered the <code>Client</code> to include a list of users:</p>
  
  <pre><code>public class Client : Entity
  {
      [Required]
      public string Name { get; set; }
  
      [Required]
      public CompanyType CompanyType { get; set; }
  
      [Required]
      public ClientStatus Status { get; set; }
  
      public virtual IList&lt;User&gt; Users { get; set; }
      public virtual User AccountManager { get; set; }
  
      public DateTime? AccessFrom { get; set; }
      public DateTime? AccessTo { get; set; }
  
      public DateTime? ClosedDate { get; set; }
  }
  </code></pre>
  
  <p>Together that made EF happy enough to pull it all out properly.</p>