c# - "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" when adding a entity to context -


i coding mvc app contains couple of many-to-many associations. 1 of them custom role <---> account association. have table full of pre-defined roles user can choose. created viewmodel holds entity model , few collections use, 1 of them roles collection. populate create form these values , resolve them again on [httppost] create action.

here relevant code:

viewmodel class:

public class accountsviewmodel {     public accounts account { get; set; }      public list<roles> roleslist { get; set; } } 

controller code:

public actionresult create() {     accountsviewmodel viewmodel = new accountsviewmodel();     viewmodel.roleslist = rolesservice.getallroles();       return view(viewmodel);   }  [httppost] [validateantiforgerytoken] public actionresult create(accountsviewmodel viewmodel) {     if (modelstate.isvalid)     {         foreach(roles role in viewmodel.roleslist)         {              if (role.isselected)              {                   roles selectedrole = rolesservice.getrole(role.id);                   viewmodel.account.roles.add(selectedrole);              }          }                  //some more code here...                  accountsservice.addaccount(viewmodel.account);     } } 

custom service class (accountsservice)

public void addaccount(accounts newaccount) {     //appdatacontext instance of <model>container     appdatacontext.accountsset.add(newaccount);     appdatacontext.savechanges(); } 

and create view:

<div class="form-group">     @html.labelfor(model => model.account.roles, htmlattributes: new { @class = "control-label col-md-2" })     <div class="col-md-10">         @for (int = 0; < model.roleslist.count; i++)         {             @html.hiddenfor(m => m.roleslist[i].id)             @html.checkboxfor(m => m.roleslist[i].isselected)             @html.labelfor(m => m.roleslist[i].isselected, model.roleslist[i].name)             <br />         }     </div> </div> 

now actual problem, each time try add new accounts object, error "an entity object cannot referenced multiple instances of ientitychangetracker." have looked @ few posts found on internet, can't associate them possible errors made in code. can me out here?

the problem roles of newaccount still attached context of roleservice. entity can attached 1 context @ time.

you can either call context.entity(...).detach() detach entity, or better role change tracking disabled , without proxy creation, give huge speed boost to:

context.configuration.autodetectchangesenabled = false; context.configuration.proxycreationenabled = false; 

even better create new context within using block inside of rolesservice.getrole(...) way context won't polluted (and slow) on time.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -