我的应用程序用一些数据填充dbcontext-这需要很长时间(大约14-20小时)。如果GetWorskpace()方法成功获取数据,则将调用DeleteModel.DeleteModelInDb()方法-此方法将删除数据库中的所有数据。删除后,dbcontext与新数据一起保存。
我在这行代码中遇到以下错误
DeleteModel.DeleteModelInDb(del);
System.Data.Entity.Core.EntityException:'基础提供者 公开赛失败。 TimeoutException:事务超时
和
TransactionException:对于该操作的状态无效 交易。
我的代码是:
static void Main(string[] args)
{
PdmContext db = new PdmContext();
Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ");
Application pdApplication = new Application();
PdWSP.Workspace pdWorkspace = (PdWSP.Workspace)pdApplication.ActiveWorkspace;
pdWorkspace.Load("H:\\workspace_dummy.sws");
using (var scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(100, 0, 0))) //100 hours
{
GetWorkspace((pdWorkspace as PdWSP.WorkspaceFolder), db); //fills dbcontext - takes about 14 hours
using (var del = new PdmContext())
{
DeleteModel.DeleteModelInDb(del);
}
db.SaveChanges();
scope.Complete();
}
db.Dispose();
}
我的DeleteModelInDb类:
public static class DeleteModel
{
public static void DeleteModelInDb(PdmContext del)
{
string nspace = "PdmExporter.Models";
List<string> items = new List<string>();
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.Namespace == nspace
select t;
var list = q.ToList();
foreach (var item in list)
{
items.Add("dbo.[" + item.Name + "]");
}
foreach (var i in items)
{
del.Database.ExecuteSqlCommand("truncate table " + i + ";");
del.Database.ExecuteSqlCommand("DBCC CHECKIDENT('" + i + "', RESEED, 1)");
}
del.SaveChanges();
}
}