上的底层提供者失败我正在使用实体框架(.NET 4.0)并将SQLite用作基础数据库,并且在尝试对数据库提交某些更改时出现异常:具有SQLite异常的实体框架:提交
底层提供程序在提交失败。
堆栈跟踪为:
System.Data.EntityException: The underlying provider failed on Commit. ---> System.Data.SQLite.SQLiteException: The database file is locked
database is locked
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavi
or behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
at System.Data.SQLite.SQLiteTransaction.Commit()
at System.Data.EntityClient.EntityTransaction.Commit()
--- End of inner exception stack trace ---
at System.Data.EntityClient.EntityTransaction.Commit()
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at MySystem.MySystemService.UpdateFollowersAndUsers() in C:\Path\To\MySystem\MySystemService.cs:line 295
我的代码非常简单:
// Gets more followers for the competitor and updates the database
List<Follower> moreFollowers = GetMoreFollowers(competitor);
// Add the new followers to the database
using (MySystemEntities db = new MySystemEntities())
{
try
{
foreach (Follower f in moreFollowers)
{
db.AddToFollowers(f);
}
db.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
的这段代码里面MyService
,它有一批5000个跟随进来,它的得到上述例外。但是,当我将相同的代码片段拉入Main
函数并只需手动添加一些追随者时,它不再抛出异常并且追随者已成功添加到我的数据库中。 显然数据库文件已被锁定,可能是导致此问题的原因?
你的意思是说,当你添加一个追随者,提交然后添加下一个但不是当第一次添加5000然后提交? – Yahia
不,我的意思是我硬编码添加5-6追随者到'moreFollower'列表,然后我使用相同的代码剪下面,它的工作原理。我试着在'foreach'循环中移动'db.SaveChanges()',但是我得到了同样的异常。 – Kiril