I am using the Entity Framework to upload a large spreadsheet (14k rows) and perform updates to my databse. If I set throught the code line by line, then each update will process as it's supposed to. However, once I step out of the code I begin to see this exception being thrown: The underlying provider failed on open
.
This is throwing me off because we use the entity framework throughout the site and it works fine, so I know our connection string is not the problem. In fact, this page will even handle the upload just fine when the spreadsheet is smaller. However, here it is for good measure.
<add name="AgentResources" connectionString="metadata=res://*/Models.AgentResources.csdl|res://*/Models.AgentResources.ssdl|res://*/Models.AgentResources.msl;provider=System.Data.SqlClient;provider connection string="data source=QUAHILSQ03\sql08A;initial catalog=AgentResourcesU01;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I'm using a using
statement that should be disposing of the dbContext
object, so I'm not sure what's going on.
Here is the method that is throwing the error. For each record, the TerritoryOnly
will be called as well as one of the Save
methods.
public static string TerritoryOnly(ZipCodeTerritory zipCode, string territory)
{
msg = string.Empty;
using (db = new AgentResources())
{
zipCode.IndDistrnId = territory;
Save(zipCode);
}
return msg;
}
private static void Save(ZipCodeTerritory autoTermZip, ZipCodeTerritory newZip)
{
db.Entry(autoTermZip).State = EntityState.Modified;
db.SaveChanges();
db.ZipCodeTerritory.Add(newZip);
db.SaveChanges();
}
private static void Save(ZipCodeTerritory zip)
{
db.Entry(zip).State = EntityState.Modified;
db.SaveChanges();
}
EDIT
The loop that is using the static methods mentioned above is itself inside a using(var scope = new TransactionScope())
statement in order to make all changes to the database atomic. Not sure if this could be contributing to the problem or not.