c# - Entity Framework not Saving Changes into Database -
i'm puzzled why code not working, should save changes database after loops when place savechanges
method inside loop, saves record database outside doesn't save anything? it's 300 ~ 1000 records
static bool lisready = false; static bool sacclready = false; static void main(string[] args) { logger("starting services"); connectdblis().wait(); connectdbsaccl().wait(); thread.sleep(1000); if (lisready & sacclready){ //start logger("services ready"); startexport().wait(); } } static async task<bool> startexport() { lis lisdb = new lis(); nrlsaccl saccldb = new nrlsaccl(); var gettestorders = await lisdb.test_orders.tolistasync(); logger("services starting"); foreach (var in gettestorders.where(x => x.entry_datetime.value.year == 2016)) { foreach (var tr in to.test_results) { foreach (var tl in tr.test_result_logs) { results_availability postresults = new results_availability { first_name = to.patient_orders.patient.first_name, middle_name = to.patient_orders.patient.middle_name, last_name = to.patient_orders.patient.last_name, birthdate = to.patient_orders.patient.birthdate, }; if (postresults.id == 0) { saccldb.results_availability.add(postresults); } else { saccldb.entry(postresults).state = entitystate.modified; } } } } await saccldb.savechangesasync(); return true; }
edit:
so limit records 100 , save changes works, 3000 records @ instant not work, solutions?
this code doesn't resolve issue, consideration problem.
note: works me when adding 1200 records , 300 modifications
static async task<bool> startexport() { using (var db = new entities()) { var appraisals = await db.appraisals.tolistasync(); db.database.commandtimeout = 300; //disabling auto detect changes enabled bring performance tweaks db.configuration.autodetectchangesenabled = false; foreach (var appraisal in appraisals.where(g => g.id > 1)) { if (appraisal.id == 10) { appraisal.appraisalname = "new name"; db.entry(appraisal).state = entitystate.added; } else { appraisal.appraisalname = "modified name"; db.entry(appraisal).state = entitystate.modified; } } db.configuration.autodetectchangesenabled = true; if (await db.savechangesasync() > 1) return true; else return false; } }
you use
db.database.commandtimeout = 300;
increase timeout of connection.entity framework 6 provides
addrange()
insert items in 1 shot, disableautodetectchangesenabled
, insert entities
in case don't want mark entites modified, ef tracks well. entity framework - why explicitly set entity state modified?
the purpose of change tracking find have changed value on attached entity , put modified state. setting state manually important in case of detached entities (entities loaded without change tracking or created outside of current context).
here have entities attached context itself
Comments
Post a Comment