title: Lookup data from second database from key in first database asp.net mvc
body: <p>I have the following error when running my get method for a list in my controller:</p>
  
  <blockquote>
    <p>The specified LINQ expression contains references to queries that are associated with different contexts.</p>
  </blockquote>
  
  <p>Having debugged the controller it is the orderby statement that produces the error</p>
  
  <p>The Method is:</p>
  
  <pre><code>    public ActionResult OwnerList()
      {                              
          var owners = (from s in db.Owners                       
                       orderby Peopledb.Posts.FirstOrDefault(x =&gt; x.PostId == s.PostId).PostName
                       select s).ToList();
  
          var viewModel = owners.Select(t =&gt; new OwnerListViewModel
          {
               Created = t.Created,
               PostName = Peopledb.Posts.FirstOrDefault(x =&gt; x.PostId == t.PostId).PostName,
               Dormant = t.Dormant,
               OwnerId = t.OwnerId,
          });
          return PartialView("_OwnerList", viewModel);
      }
  </code></pre>
  
  <p>The Class for Owner in the first database is, dbcontext = IARContext:</p>
  
  <pre><code>public class Owner
  {
      public int OwnerId { get; set; }
  
      [Column(TypeName = "int")]
      public int PostId { get; set; }
  
      [Column(TypeName = "bit")]
      public bool Dormant { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime Created { get; set; }
  
      public virtual ICollection&lt;Asset&gt; Assets { get; set; }
  
      public People.Models.Post Post { get; set; }
  }
  </code></pre>
  
  <p>The Class for Post in the second database is, dbcontext = PeopleContext:</p>
  
  <pre><code>public class Post
  {
      public int PostId { get; set; }
  
      [StringLength(50)]
      [Column(TypeName = "nvarchar")]
      public string PostName { get; set; }
  
      [Column(TypeName = "bit")]
      public bool Dormant { get; set; }
  
      [StringLength(350)]
      [Column(TypeName = "nvarchar")]
      public string Description { get; set; }
  
      public virtual ICollection&lt;Contract&gt; Contracts { get; set; }
  
      public virtual ICollection&lt;Owner&gt; Owners { get; set; }
  }
  </code></pre>
  
  <p>I am trying to look up PostName from Post in the People db when displaying a list of Owners from the IAR db</p>