C# Sqlite实体框架无法在更新时打开文件
我完全被这个错误弄糊涂了。在读取数据库文件时,我经常遇到无法打开文件错误。我发现我有一个文件路径问题,并修复了它。现在,当我尝试更新数据库时,我得到了它。这发生在阅读之后。为什么它在更新时无法打开,但在读取时却无法打开C# Sqlite实体框架无法在更新时打开文件,c#,entity-framework,sqlite,C#,Entity Framework,Sqlite,我完全被这个错误弄糊涂了。在读取数据库文件时,我经常遇到无法打开文件错误。我发现我有一个文件路径问题,并修复了它。现在,当我尝试更新数据库时,我得到了它。这发生在阅读之后。为什么它在更新时无法打开,但在读取时却无法打开 using (var con = new UploadContext()) { upload = con.Uploads.SingleOrDefault(u => u.HashId == hashId); if (upload == null) {
using (var con = new UploadContext())
{
upload = con.Uploads.SingleOrDefault(u => u.HashId == hashId);
if (upload == null)
{
upload = new Upload()
{
HashId = hashId,
StartTime = DateTime.UtcNow,
UploadId = generate()
};
con.Uploads.Add(upload);
con.SaveChanges();
}
}
...
错误如下所示:
System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at FileServer.Services.UploadService.GetUpload(String fileName, Int32 size, Int32 userId) in ...\\UploadService.cs:line 67
at FileServer.Controllers.FilesController.PreUpload(PreUploadInfoModel model) in ...\FilesController.cs:line 31
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
InnerException: System.Data.Entity.Core.UpdateException
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
InnerException: System.Data.SQLite.SQLiteException
HResult=-2147467259
Message=unable to open database file
unable to open database file
Source=System.Data.SQLite
ErrorCode=14
StackTrace:
at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
InnerException:
用户代码未处理System.Data.Entity.Infrastructure.DbUpdateException
HResult=-2146233087
消息=更新条目时出错。有关详细信息,请参见内部异常。
Source=EntityFramework
堆栈跟踪:
位于System.Data.Entity.Internal.InternalContext.SaveChanges()处
在…\\UploadService.cs:第67行的FileServer.Services.UploadService.GetUpload(字符串文件名,Int32大小,Int32用户ID)处
在…\filecontroller.cs中的FileServer.Controllers.filecontroller.PreUpload(PreUploadInfoModel模型)处:第31行
在lambda_方法(闭包、对象、对象[])
在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c\u DisplayClass10.b\u 9(对象实例,对象[]方法参数)
位于System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2参数,CancellationToken CancellationToken)
InnerException:System.Data.Entity.Core.UpdateException
HResult=-2146233087
消息=更新条目时出错。有关详细信息,请参见内部异常。
Source=EntityFramework
堆栈跟踪:
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()处
在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 Func,IDBEcutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions选项、IDBEcutionStrategy executionStrategy、Boolean startLocalTransaction)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions选项,布尔值executeInExistingTransaction)
位于System.Data.Entity.Internal.InternalContext.SaveChanges()处
InnerException:System.Data.SQLite.SQLiteException
HResult=-2147467259
消息=无法打开数据库文件
无法打开数据库文件
Source=System.Data.SQLite
错误代码=14
堆栈跟踪:
在System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
在System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
位于System.Data.SQLite.SQLiteDataReader.NextResult()处
在System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd,CommandBehavior)
在System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior)
在System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior)
在System.Data.Entity.Infrastructure.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget目标,Func`3操作,TInterceptionContext拦截Context,操作`3执行,操作`3执行)
位于System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext interceptionContext)
在System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues,List`1 generatedValues)
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()处
内部异常:
sqlite配置:
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<connectionStrings>
<add name="UploadContext" connectionString="Data Source=|DataDirectory|\fileserver.sqlite;Version=3;" providerName="System.Data.SQLite" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
</DbProviderFactories>
</system.data>
SQLite一次只能处理一次写入,但可以同时处理多个读取
请为SQLite文件夹的
应用程序池
用户授予应用程序数据
目录的写入权限
我正在进行一次读取,然后尝试进行一次插入,结果出现无法打开文件错误。但它并没有说它被锁上了。但不管怎样,我只写了一次,所以我不应该有锁定问题,对吗?是的,如果你写了一次,那么你应该没事。顺便说一下,如果要为insert语句打开一个新连接,请确保在那里也有正确的文件路径。我的意思是如果你有
SQLiteConnection m_dbConnection;m_dbConnection=newsqliteconnection(“数据源=myDB.db;版本=3;”);m_dbConnection.Open()代码>语句写在多个地方,确保所有地方的路径都正确。它应该支持多个连接吗?刚才有多个写操作。也许这可以回答您最后的评论。据我所知,这是一个web应用程序。检查用于应用程序池的用户是否对db file.Worked具有写入权限。我已经在.db
文件上授予了Modify
和Write
,但我没有意识到父目录也需要它们。