i creating context in method inside entity check not tracking when try save in calling code context throws exception.
this calling code in main context want save:
var esptoprocess = db.rootdomainemailseriesprogresses; foreach (var esp in esptoprocess) { bool carryon = esp.movenext(); db.savechanges(); //exception if (!carryon) continue; //--> rest of code }
this methods inside rootdomainemailseriesprogress
class.
public bool movenext() { if (this.completedtargets == null) this.completedtargets = new list<emailaddress>(); if (this.currenttarget != null) { this.completedtargets.add(this.currenttarget); this.currenttarget = null; } this.currentprogress = ""; if (this.rootdomain.contactfilter != rootdomain.contactfiltertype.none) { this.status = emailseriesstatus.aborted; return false; } var alltargets = rootdomainemailmanager.sortdomainsemailsbydesirability(this.rootdomain.id); var todo = alltargets.except(this.completedtargets); if (todo.count() < 1) { this.status = emailseriesstatus.completed; return false; } list<string> targetemaillist = alltargets.select(e => e.email).tolist(); list<emailfilter> emailfilters = this.getfilters(alltargets); if (emailfilters.any(x => x.filter == emailfiltertype.unsubscribe || x.filter == emailfiltertype.responded || x.filter == emailfiltertype.manualcontactonly)) { this.status = emailseriesstatus.aborted; if (this.rootdomain.contactfilter == 0) this.rootdomain.contactfilter = rootdomain.contactfiltertype.hasassociatedemailfilter; return false; } this.currenttarget = todo.first(); return true; } private list<emailfilter> getfilters(list<emailaddress> alltargets) { using (var db = new placedbcontext()) { db.configuration.autodetectchangesenabled = false; db.configuration.lazyloadingenabled = false; var targetemaillist = alltargets.select(e => e.email).tolist(); return db.emailfilters.asnotracking().where(x => targetemaillist.contains(x.email)).tolist(); } }
it throws out exception:
the relationship between 2 objects cannot defined because attached different objectcontext objects.
i can't see why esp
gets attached other context. need context briefly, how kill off stops causing me issues?
that because there difference dbcontext instances in foreach loop , getfilters method
you can retry code
var esptoprocess = db.rootdomainemailseriesprogresses; foreach (var esp in esptoprocess) { bool carryon = esp.movenext(db); db.savechanges(); //exception if (!carryon) continue; //--> rest of code } public bool movenext(dbcontext db) { if (this.completedtargets == null) this.completedtargets = new list<emailaddress>(); if (this.currenttarget != null) { this.completedtargets.add(this.currenttarget); this.currenttarget = null; } this.currentprogress = ""; if (this.rootdomain.contactfilter != rootdomain.contactfiltertype.none) { this.status = emailseriesstatus.aborted; return false; } var alltargets = rootdomainemailmanager.sortdomainsemailsbydesirability(this.rootdomain.id); var todo = alltargets.except(this.completedtargets); if (todo.count() < 1) { this.status = emailseriesstatus.completed; return false; } list<string> targetemaillist = alltargets.select(e => e.email).tolist(); list<emailfilter> emailfilters = this.getfilters(alltargets, db); if (emailfilters.any(x => x.filter == emailfiltertype.unsubscribe || x.filter == emailfiltertype.responded || x.filter == emailfiltertype.manualcontactonly)) { this.status = emailseriesstatus.aborted; if (this.rootdomain.contactfilter == 0) this.rootdomain.contactfilter = rootdomain.contactfiltertype.hasassociatedemailfilter; return false; } this.currenttarget = todo.first(); return true; } private list<emailfilter> getfilters(list<emailaddress> alltargets, dbcontext db) { db.configuration.autodetectchangesenabled = false; db.configuration.lazyloadingenabled = false; var targetemaillist = alltargets.select(e => e.email).tolist(); return db.emailfilters.asnotracking().where(x => targetemaillist.contains(x.email)).tolist();
}
Comments
Post a Comment