问题描述
我正在使用实体框架(.NET 4.0)与SQLite作为底层数据库,当我尝试对数据库进行一些更改时,我收到异常:
I'm using the Entity Framework (.NET 4.0) with SQLite as the underlying database and I get an exception when I try to commit some changes to the database:
底层提供程序提交失败。
堆栈跟踪是:
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
,它有一批5,000个追随者,它得到了上述例外。但是,当我将相同的代码片段拉到 Main
函数中,只需手动添加几个关注者,那么它不再抛出异常,并且追踪者成功添加到我的数据库。 显然数据库文件被锁定,可能导致这个问题?
This snippet of code is inside MyService
, it comes in with a batch of 5,000 followers and it's getting the above exception. However, when I pull the same snippet of code out into the Main
function and just manually add a few followers, then it no longer throws the exception and the followers are successfully added to my database. Apparently the database file is locked, what may be causing this issue?
推荐答案
我刚刚发现在另一个SO问题中的答案:
I just found out the answer in another SO question: 'The database file is locked' with System.Data.SQLite
讽刺的是,OP也回答了自己的问题:)。
Ironically, the OP answers his own question too :).
这篇关于使用SQLite异常的实体框架:提交时底层提供程序失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!