title: UnitofWork + DependencyResolver will raise an exception when calling .Save() "The relationship between the two objects cannot be defined"
body: <p>I am working on an asp.net mvc 5.2 web application + Entity framework 6. and i have defined 2 repositories for two entities; SecurityRole and Staff, then i defined a unit of work between them as follow:-</p>
  
  <pre><code>public class UnitOfWork : IDisposable
      {
          private SkillManagementEntities context = new SkillManagementEntities();
          private SkillRepository skillRepository;
          private StaffRepository staffRepository;
          private SecurityRoleRepository securityroleRepository;
          private CustomerRepository customerRepository;
          private SkillVersionHistoryRepository SVH;
          private LinkToKBRepository linkToKBRepository;
          public SkillRepository SkillRepository
          {
              get
              {
  
                  if (this.skillRepository == null)
                  {
                      this.skillRepository = new SkillRepository(context);
                  }
                  return skillRepository;
              }
          }
  
          public StaffRepository StaffRepository
          {
              get
              {
  
                  if (this.staffRepository == null)
                  {
                      this.staffRepository = new StaffRepository(context);
                  }
                  return staffRepository;
              }
          }
  
          public SecurityRoleRepository SecurityRoleRepository
          {
              get
              {
  
                  if (this.securityroleRepository == null)
                  {
                      this.securityroleRepository = new SecurityRoleRepository(context);
                  }
                  return securityroleRepository;
              }
          }
  
          public async Task Save()
          {
              await context.SaveChangesAsync();
          }
  
          private bool disposed = false;
  
          protected virtual void Dispose(bool disposing)
          {
              if (!this.disposed)
              {
                  if (disposing)
                  {
                      context.Dispose();
                  }
              }
              this.disposed = true;
          }
  
          public void Dispose()
          {
              Dispose(true);
              GC.SuppressFinalize(this);
          }
      }
  }
  </code></pre>
  
  <p>now inside the SecurityRole controller i have the following action method , to assign Staffs to security role:-</p>
  
  <pre><code>public class SecurityRoleController : Controller
      {
  
      UnitOfWork uniteofwork = new UnitOfWork(); 
  [HttpPost]
          [ValidateAntiForgeryToken]
  
          public async Task&lt;ActionResult&gt; AssignStaffsToSR(int? id , int[] selectedStaffsids)
          {
  
  
              var dbsecurityrole = new SecurityRole();
              try
              {
  
                  dbsecurityrole = await uniteofwork.SecurityRoleRepository.FindSecurityRole(id.Value,sr=&gt;sr.Staffs);
                uniteofwork.SecurityRoleRepository.UpdateSecurityRoleUsers(selectedStaffsids, dbsecurityrole);
                await uniteofwork.Save();
                  TempData["message"] = string.Format("{0} User/s have been Assigned", String.Empty);
                  if (Request.IsAjaxRequest())
                  {
                      return Json(new { ISsuccess = true });
                  }
  
                  return RedirectToAction("Details", new { id = id });
  
  
              }
              catch (Exception e)
              {
                  ModelState.AddModelError(string.Empty, "Error occured");
              }
  
  
  
                   return View(await uniteofwork.SecurityRoleRepository.populateAssignStaff(dbsecurityrole)); 
              }
  </code></pre>
  
  <p>which calls the following repository method:-</p>
  
  <pre><code>public class SecurityRoleRepository :  ISecurityRoleRepository , IDisposable
          {
           private SkillManagementEntities context;
           private IStaffRepository staffrepo = (IStaffRepository)DependencyResolver.Current.GetService(typeof(IStaffRepository));
           public SecurityRoleRepository(SkillManagementEntities context)
              {
                  this.context = context;
              }
  public void UpdateSecurityRoleUsers(int[] selectedUsers, SecurityRole securityRole)
                {
                    var dbSecurityRolestaffs = securityRole.Staffs.ToList();
                    var activestaffs = staffrepo.GetActiveStaff().ToList();
  
                    foreach (var s in activestaffs)
                    {
  
                        if (selectedUsers.Contains(s.StaffID))
                        {
                            if (!dbSecurityRolestaffs.Any(a =&gt; a.StaffID == s.StaffID))
                            {
  
                                securityRole.Staffs.Add(s);
                            }
                        }
                        else
                        {
                            if (dbSecurityRolestaffs.Any(a =&gt; a.StaffID == s.StaffID))
                            {
                                securityRole.Staffs.Remove(s);
                            }
  
  
  
                        }
                    }
  
                }
  </code></pre>
  
  <p>and i have defined the following dependency resolver between the repositories:-</p>
  
  <pre><code>public class YourDependencyResolverClass : IDependencyResolver
  {
      private IKernel kernel;
  
      public YourDependencyResolverClass()
      {
          kernel = new StandardKernel();
          AddBindings();
      }
  
      public object GetService(Type serviceType)
      {
          return kernel.TryGet(serviceType);
      }
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          return kernel.GetAll(serviceType);
      }
  
  
      private void AddBindings()
      {
          kernel.Bind&lt;ISkillRepository&gt;().To&lt;SkillRepository&gt;();
          kernel.Bind&lt;IStaffRepository&gt;().To&lt;StaffRepository&gt;();
          kernel.Bind&lt;ISecurityRoleRepository&gt;().To&lt;SecurityRoleRepository&gt;();
          kernel.Bind&lt;ICustomerRepository&gt;().To&lt;CustomerRepository&gt;();
          kernel.Bind&lt;ISkillVersionHistoryRepository&gt;().To&lt;SkillVersionHistoryRepository&gt;();
       }
  </code></pre>
  
  <p>but currently when i call the <code>await uniteofwork.Save();</code> inside my action method i got the following exception:-</p>
  
  <blockquote>
    <p>The relationship between the two objects cannot be defined because
    they are attached to different ObjectContext objects.</p>
  </blockquote>
  
  <p>although i am using a UnitofWork class which should pass the same objectcontext between the repositories and entities ??</p>