After a lot of hair pulling I've decided to join the stack overflow community and post my first question. When I try to update I get this error "entity cannot be referenced by multiple instances of IEntityChangeTracker".
Here's the IRepository.
public interface IRepository<T>
{
void Update(T item);
}
And the Repository.
public class Repository<T> : IRepository<T> where T : class
{
private readonly DbContext _dbContext;
public Repository(DbContext context)
{
this._dbContext = context;
}
public virtual void Update(T item)
{
_dbContext.Entry(item).State = EntityState.Modified; // This is where the error happens
_dbContext.SaveChanges();
}
}
And Model class.
Repository<Model> _model = new Repository<Model>(new ModelContext());
public void Update(Model item)
{
this.Modified = DateTime.Now;
_model.Update(item);
}
}
And Model context.
public class ModelContext : DbContext
{
public ModelContext()
: base("name=DbContext")
{
}
public DbSet<ModelContext> Model { get; set; }
}
And also the Controller
public class ModelController : Controller
{
Model _model = new Model();
public ActionResult Edit(int id, string name)
{
Model item = _model.GetById(id);
if (Request.IsAjaxRequest())
{
item.Name = name;
item.Update(item);
}
return View();
}
}
And now it's the big question if some savior out there can make my day.