title: An entity object cannot be referenced by multiple instances of IEntityChangeTracker error
body: <p>I understand there are a lot of duplicates to this question, but I couldn't find one that fits my scenario.</p>
  
  <p>So I am using the ASP.NET MVC 4 + Entity Framework + Ninject using repository pattern (I see many mentions of repository + unit of work pattern? That could be a potential fix to my problem but I don't know how to  implement it). </p>
  
  <p>When I try to add a new post, I get "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" error on the following line of code</p>
  
  <pre><code>context.Posts.Add(post);
  </code></pre>
  
  <p>Here is my full implementation:</p>
  
  <p>Concrete repository</p>
  
  <pre><code>public class EFBlogRepository : IBlogRepository
  {
      private readonly EFDbContext context;
  
      public EFBlogRepository(EFDbContext dbcontext)
      {
          context = dbcontext;
      }
  
      //add post
      public int AddPost(Post post)
      {
          context.Posts.Add(post);
          context.SaveChanges();
          return post.PostID;
      }
  
      public Category Category(int id)
      {
          return context.Categories.FirstOrDefault(c =&gt; c.CategoryID == id);
      }
  
      public Tag Tag(int id)
      {
          return context.Tags.FirstOrDefault(t =&gt; t.TagID == id);
      }
  }
  </code></pre>
  
  <p>Interface</p>
  
  <pre><code>public interface IBlogRepository
  {
      int AddPost(Post post);
      Category Category(int id);
      Tag Tag(int id);
  }
  </code></pre>
  
  <p>My controller</p>
  
  <pre><code>public class AdminController : Controller
  {
      private IBlogRepository repository;
  
      public AdminController(IBlogRepository repo)
      {
          repository = repo;
      }
  
      [HttpPost]
      public ContentResult AddPost(Post post)
      {
          string json;
  
          ModelState.Clear();
  
          if (TryValidateModel(post))
          {
              var id = repository.AddPost(post);
  
              json = JsonConvert.SerializeObject(new
              {
                  id = id,
                  success = true,
                  message = "Post added successfully."
              });
          }
          else
          {
              json = JsonConvert.SerializeObject(new
              {
                  id = 0,
                  success = false,
                  message = "Failed to add the post."
              });
          }
          return Content(json, "application/json");
      }
  }
  </code></pre>
  
  <p>I don't think any of the above are the root of the problem, I think the problem is in my custom model binder</p>
  
  <pre><code>public class PostModelBinder : DefaultModelBinder
  {
      public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
          var post = (Post)base.BindModel(controllerContext, bindingContext);
  
          var repository = DependencyResolver.Current.GetService&lt;IBlogRepository&gt;();
  
          if (post.Category != null)
              post.Category = repository.Category(post.Category.CategoryID);
  
          var tags = bindingContext.ValueProvider.GetValue("Tags").AttemptedValue.Split(',');
  
          if (tags.Length &gt; 0)
          {
              post.Tags = new List&lt;Tag&gt;();
  
              foreach (var tag in tags)
              {
                  post.Tags.Add(repository.Tag(int.Parse(tag.Trim())));
              }
          }
  
          return post;
      }
  }
  </code></pre>
  
  <p>and my global.asax.cs</p>
  
  <pre><code>ModelBinders.Binders.Add(typeof(Post), new PostModelBinder());
  </code></pre>
  
  <p>This is my Ninject dependency resolver</p>
  
  <pre><code>public class NinjectDependencyResolver: IDependencyResolver
  {
      private IKernel kernel;
  
      public NinjectDependencyResolver()
      {
          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;IBlogRepository&gt;().To&lt;EFBlogRepository&gt;();
          kernel.Bind&lt;IAuthProvider&gt;().To&lt;FormsAuthProvider&gt;();
      }
  }
  </code></pre>