title: Multiple database contexts when using repository pattern
body: <p>I am a bit lost right now... I've never seen this much divergent information regarding solution to the problem. But let us start from the beginning.</p>
  
  <p>I am using ASP.NET MVC with Repositories injected to Controllers, thanks to the Ninject. I have 2 simple Entities: Admin with a list of created blog entries and Entries with one virtual Admin field.</p>
  
  <p><strong>Admin:</strong></p>
  
  <pre><code>public class Admin
  {
      [Key, ScaffoldColumn(false)]
      public int Id { get; set; }
  
      [Required(ErrorMessage = "Zły login.")]
      [StringLength(20), MinLength(3)]
      [RegularExpression(@"^[a-zA-Z0-9]*$", ErrorMessage = "Special characters are not allowed.")]
      public string Login { get; set; }
  
      [Required(ErrorMessage = "Złe hasło.")]
      [StringLength(20, MinimumLength = 3)]
      [DataType(DataType.Password)]
      [Display(Name = "Hasło")]
      public string Password { get; set; }
  
      public virtual List&lt;Entry&gt; CreatedEntries { get; set; } // napisane aktualności przez danego admina
  }
  </code></pre>
  
  <p><strong>Entry:</strong></p>
  
  <pre><code>public class Entry
  {
      [Key, ScaffoldColumn(false)]
      public int Id { get; set; }
  
      [StringLength(200, MinimumLength = 2)]
      [DataType(DataType.Text)]
      [Display(Name = "Tytuł")]
      public string Title { get; set; }
  
      [Required, StringLength(2000), MinLength(3)]
      [Display(Name = "Treść")]
      [UIHint("tinymce_jquery_full"), AllowHtml]
      public string Text { get; set; }
  
      public virtual Admin Admin { get; set; }
  }
  </code></pre>
  
  <p>You probably know where it is going, since this problem is... "classic" on stackoverflow.</p>
  
  <p>In the Controller I want to bind one object to another:</p>
  
  <pre><code>entry.Admin = repAdmins.GetAdmin(User.Identity.Name);
  
  repEntries.AddEntry(entry);
  </code></pre>
  
  <p>In the repository:</p>
  
  <pre><code>public void AddEntry(Entry entry)
      {
          db.Entries.Add(entry);
          db.SaveChanges();
      }
  </code></pre>
  
  <p>Of course I can't do that, because of famous "An entity object cannot be referenced by multiple instances of IEntityChangeTracker", which is a result of having separate database contexts in each repository.</p>
  
  <p>When I was searching for a solution I already knew that probably the best way to solve it is to use one common context. And then I discovered Unit Of Work pattern. But here's when the real problems starts.</p>
  
  <ol>
  <li>On many sites the solution to this is a bit different.</li>
  <li>The repositories must have common generic interface (which I don't want to use, because I don't need to have each CRUD operation on each Entity, plus sometimes I need to have extra methods like "IfExists", etc.)</li>
  <li>On few sites I've read that this whole abstraction is not needed, since abstraction is already provided with Entity Framework and UoW is implemented in DbContext (whatever that means)</li>
  <li>The Unit Of Work pattern (at least from examples on the internet) seems to be a real pain for me...</li>
  </ol>
  
  <p>I need some guidance... I learn ASP.NET MVC for only a year. For me it seems like it's a "triumph of form over content". Because... <strong>What I simply need is to bind one object to another.</strong> I'm starting to think that it was better when I simply had a context object in the Controller and I didn't need to build Eiffel Tower to achieve what's mentioned above :\ However I like idea of repositories...</p>