c# - Getting The connection does not support MultipleActiveResultSets when using Dapper.SimpleCRUD in a forEach -


i have following code:

var test = new fallenvironmentalcondition[] {     new fallenvironmentalcondition {id=40,fallid=3,environmentalconditionid=1},     new fallenvironmentalcondition {id=41,fallid=3,environmentalconditionid=2},     new fallenvironmentalcondition {id=42,fallid=3,environmentalconditionid=3} }; test.tolist().foreach(async x => await conn.updateasync(x)); 

i getting

invalidoperationexception: connection not support multipleactiveresultsets

i don't understand awaiting each update why getting error.

note: have no control on connection string can't turn mars on.

that code starts task each item in list, not wait each task complete before starting next one. inside each task waits update complete. try

 enumerable.range(1, 10).tolist().foreach(async => await task.delay(1000).continuewith(t => console.writeline(datetime.now))); 

which equivalent

    foreach (var in enumerable.range(1, 10).tolist() )     {         var task = task.delay(1000).continuewith(t => console.writeline(datetime.now));     } 

if you're in non-async method have wait(), not await each task. eg

    foreach (var in enumerable.range(1, 10).tolist() )     {         var task = task.delay(1000).continuewith(t => console.writeline(datetime.now));         //possibly other stuff on thread         task.wait(); //wait task complete     } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -