I'm following the code at Fastest Way of Inserting in Entity Framework
I'm getting this error message when I run this code and I can't tell what I'm doing wrong since I copied it exactly from the page. Any ideas on what I'm doing wrong?
Error message: The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements.
The code seems to run fine for awhile but then it will stop almost at the same time at the line I marked below. I'm not sure what is going on.
Stacktrace:
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.b__2(UpdateTranslator ut) at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction) at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() at System.Data.Entity.Core.Objects.ObjectContext.b__35() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.b__27() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges()
public static async Task startInitialMarketSymbolsDownload(string market)
{
try
{
List<string> symbolList = new List<string>();
symbolList = getStockSymbols(market);
var historicalGroups = symbolList.Select((x, i) => new { x, i })
.GroupBy(x => x.i / 50)
.Select(g => g.Select(x => x.x).ToArray());
List<DailyAmexData>[] amexListArray = await Task.WhenAll(historicalGroups.Select(g => Task.Run(() => getLocalHistoricalStockData(g, market))));
using (TransactionScope scope = new TransactionScope())
{
ooplesfinanceEntities context = null;
List<DailyAmexData> amexList = null;
try
{
amexList = new List<DailyAmexData>();
foreach (List<DailyAmexData> singleList in amexListArray)
{
amexList.AddRange(singleList);
}
context = new financeEntities();
context.Configuration.AutoDetectChangesEnabled = false;
int count = 0;
foreach (var amexData in amexList)
{
++count;
context = AddToContext(context, amexData, count, 100, true);
}
context.SaveChanges();
Console.WriteLine("Everything is finished!");
}
finally
{
if (context != null)
context.Dispose();
}
scope.Complete();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
}
}
private static financeEntities AddToContext(financeEntities context, DailyAmexData entity, int count, int commitCount, bool recreateContext)
{
context.Set<DailyAmexData>().Add(entity);
if (count % commitCount == 0)
{
context.SaveChanges(); **// seems to happen at this line**
Console.WriteLine("100 items saved");
if (recreateContext)
{
context.Dispose();
context = new financeEntities();
context.Configuration.AutoDetectChangesEnabled = false;
}
}
return context;
}