title: How to have 2 related objects stored in different databases using Entity Framework
body: <p>I'm redoing an old application using C#, .net 4.5, ASP.NET MVC 5 and Entity Framework 6.</p>
  
  <p>Because this web app will be used at the same time the old app, it needs to share the same databases.</p>
  
  <p>To save me some time I've generated my model based on the existing databases. (to do this I first gathered all tables in the same database, defined their relationships, generated the model based on that new database with every table and then use the old databases to run my code)</p>
  
  <p><a href="https://i.stack.imgur.com/HkuUm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HkuUm.png" alt="enter image description here"></a></p>
  
  <p>To clarify my problem I created this simple example.</p>
  
  <p>A person can belong to many groups.</p>
  
  <p>One table <code>Persons</code> is in DB1 and the table <code>Groups</code> is in DB2.</p>
  
  <p>The relation between these 2 tables doesn't exist because they are in different databases. But I should be able to know to which Groups a Person belongs to and which persons are in a given group.</p>
  
  <p>The generated code for the class persons is:</p>
  
  <pre><code>public partial class Persons
  {
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int ID { get; set; }
  
      [Required]
      [StringLength(50)]
      [Display(Name = "Person Name")]
      public string Name { get; set; }
  
      public int GroupID { get; set; }
  
      public virtual Groups Groups { get; set; }
  }
  </code></pre>
  
  <p>and the Groups class:</p>
  
  <pre><code>public partial class Groups
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
      public Groups()
      {
          Persons = new HashSet&lt;Persons&gt;();
      }
  
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int ID { get; set; }
  
      [Required]
      [StringLength(50)]
      [Display(Name = "Group Name")]
      public string Name { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;Persons&gt; Persons { get; set; }
  }
  </code></pre>
  
  <p>then I created 2 contexts one for each database</p>
  
  <pre><code>&lt;add name="ModelContext1" connectionString="data source=PROG-PC\SQLEXPRESS;initial catalog=DB1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /&gt;
  &lt;add name="ModelContext2" connectionString="data source=PROG-PC\SQLEXPRESS;initial catalog=DB2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>and the respective context classes (I'm not sure if I should keep the code related to the foreign keysm since they dont actually exist)</p>
  
  <pre><code>public partial class ModelContext1 : DbContext
      {
          public ModelContext1()
              : base("name=ModelContext1")
          {
          }
  
      public virtual DbSet&lt;Persons&gt; Persons { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Groups&gt;()
              .HasMany(e =&gt; e.Persons)
              .WithRequired(e =&gt; e.Groups)
              .HasForeignKey(e =&gt; e.GroupID)
              .WillCascadeOnDelete(false);
      }
  }
  
  
  
  public partial class ModelContext2 : DbContext
      {
          public ModelContext2()
              : base("name=ModelContext2")
          {
          }
  
      public virtual DbSet&lt;Groups&gt; Groups { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Groups&gt;()
              .HasMany(e =&gt; e.Persons)
              .WithRequired(e =&gt; e.Groups)
              .HasForeignKey(e =&gt; e.GroupID)
              .WillCascadeOnDelete(false);
      }
  }
  </code></pre>
  
  <p>My first problem is my PersonControler:</p>
  
  <pre><code>public class PersonsController : Controller
      {
      private ModelContext1 db = new ModelContext1();
      private ModelContext2 db2 = new ModelContext2();
  
      // GET: Persons
      public ActionResult Index()
      {
          var persons = db.Persons.Include(p =&gt; p.Groups);
          return View(persons.ToList());
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/gRobi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gRobi.png" alt="enter image description here"></a></p>
  
  <p>(keep im mind I'm new to MVC and EntityFramework)</p>
  
  <p>How can I solve this problem?</p>
  
  <p>What other considerations should I take?</p>