title: RIA Services: Entity Framework Reference Entities
body: <p>I'm attempting to use RIA services with a Entity Framework back end. The Model was auto generated by the designer. (VS 2010, .NET 4.0, EF 4.0, SilverLight 4.0)
  The issue that I'm running into is that Reference Properties are being loaded as expected at the Service Layer, but when that information is passed over to the SilverLight App the Reference Entity becomes NULL.</p>
  
  <p>I've used two approaches to populating the Reference Entity:</p>
  
  <pre><code>public Employee GetEmployeeByID(int employeeID)
    {
     var result = this.ObjectContext.Employees
      .Include("EmployeeRoles")
      .Where(emp =&gt; emp.EmployeeID == employeeID)
      .FirstOrDefault();
     return result;
    }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>public Employee GetEmployeeByID(int employeeID)
    {
     var result = this.ObjectContext.Employees
      .Where(emp =&gt; emp.EmployeeID == employeeID)
      .FirstOrDefault();
  
     if (result != null &amp;&amp; result.EmployeeRoleReference.IsLoaded == false)
     {
      result.EmployeeRoleReference.Load();
     }
     return result;
    }
  </code></pre>
  
  <p>Both methods appear to correctly populate the Reference objects when examining the objects prior to the return.
  However when I attempt to reference the Employee Object from the UI via RIA Services, the EmployeeRole referenced entity is NULL.</p>
  
  <p>On the Front End I'm calling:</p>
  
  <pre><code>public void LoadEmployeeProfile()
    {
     int empID = WebContext.Current.User.EmployeeID;
     LoadOperation&lt;Employee&gt; loadEmployee = _appcontext.Load(_appContext.GetEmployeeByIDQuery(empID));
     loadEmployee.Complete += new System.EventHandler(loadEmployee_Completed);
    }
  
    void LoadEmployee_Completed(object sender, System.EventArgs e)
    {
     LoadOperation&lt;Employee&gt; loadEmployee = sender as LoadOperation&lt;Employee&gt;;
     if (loadEmployee == null)
      return;
  
     loadEmployee.Completed -= LoadEmployee_Completed;
     foreach (Employee employee in loadEmployee.Entities)
     {
      this.EmployeeProfile == employee;
      break;
     }
    }
  </code></pre>
  
  <p>This appears to be all textbook type stuff, but I haven't been able to chase down a lead as to why the RIA services isn't populating the Reference entity as I would have expected it too.</p>