gpt4 book ai didi

c# - 带有 SQLite 异常 : The underlying provider failed on Commit 的 Entity Framework

转载 作者:行者123 更新时间:2023-11-30 18:02:18 28 4
gpt4 key购买 nike

我正在使用 Entity Framework (.NET 4.0) 和 SQLite 作为底层数据库,当我尝试对数据库进行一些更改时出现异常:

The underlying provider failed on Commit.

堆栈跟踪是:

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 函数中并手动添加一些关注者时,它不再抛出异常并且关注者已成功添加到我的数据库中。 显然数据库文件被锁定,可能是什么导致了这个问题?

最佳答案

我刚刚在另一个 SO 问题中找到了答案:'The database file is locked' with System.Data.SQLite

具有讽刺意味的是,OP 也回答了他自己的问题 :)。

关于c# - 带有 SQLite 异常 : The underlying provider failed on Commit 的 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8192378/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com