c# - EntityFramework The wait operation timed out on long running task -
i'm using ef5 migrate data 1 database another. use sql need other functionality (like creating users in membershipprovider) , hoping in ef. i'm migrating 100k rows , using so:
using (var connection = new sqlconnection(connectionstring)) { using(var command = new sqlcommand(commandtext, connection)) { connection.open(); using (sqldatareader reader = command.executereader()) { while (reader.read()) { var employer = new employer(); employer.ean = reader["ean"].tostring(); employer.name = getstring(reader["empname"]); employer.taxmailingaddress = new address { streetaddress = getstring(reader["street"]), city = getstring(reader["city"]), state = getstring(reader["usstate"]), zipcode = getstring(reader["zip"]) }; employer.taxmailingaddress.saveorupdate(); employer.saveorupdate(); // timeout happens string dba = getstring(reader["dba"]); if (!string.isnullorwhitespace(dba)) { employer.adddba(new employerdba { name = dba }); } string email = getstring(reader["email"]); if (!string.isnullorwhitespace(email)) { var user = createnewuser(email); if (user != null) { user.addauthorizedemployer(employer); user.addrole(employerrole.admin, employer, true); } } } } } } my saveorupdate method pretty straight forward:
public void saveorupdate() { using (var db = new mycontext()) { if (db.employers.firstordefault(x => x.ean == ean && x.id != id) != null) throw new exception("an employer ean has been registered."); var employer = new employer(); if (id == 0) { db.employers.add(employer); employer.createdby = statics.getcurrentusername(); employer.datecreated = datetime.now; } else { employer = db.employers.firstordefault(x => x.id == id); employer.modifiedby = statics.getcurrentusername(); employer.datemodified = datetime.now; } employer.ean = ean; employer.name = name; if (taxmailingaddress != null) employer.taxmailingaddress = db.addresses.firstordefault(x => x.id == taxmailingaddress.id); if (singleseparationstatementaddress != null) employer.singleseparationstatementaddress = db.addresses .firstordefault(x => x.id == singleseparationstatementaddress.id); db.savechanges(); id = employer.id; } } the task should take 2.5 hours complete. however, after running many thousands of rows, sometime 80k, few 7k, "the wait operation timed out" exception, on employer.saveorupdate();. have how close employer.taxmailingaddress.saveorupdate();? there "wait transaction complete" deal? perhaps make sure connection valid , if not try recreating or something? help.
the problem ended being initial database connection being used connect other database, getting data import, timing out. ended looping through of reader.read() statements , putting array. looped through array process , save data new database.
Comments
Post a Comment