Generic entity cannot be referenced by multiple instances of IEntityChangeTracker -
after lot of hair pulling i've decided bring together stack overflow community , post first question. when seek update error "entity cannot referenced multiple instances of ientitychangetracker".
here's irepository.
public interface irepository<t> { void update(t item); }
and repository.
public class repository<t> : irepository<t> 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; // 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 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); } homecoming view(); } }
and it's big question if savior out there can create day.
entity-framework generics repository instances
No comments:
Post a Comment