c# - Sqlite Entity Framework 无法在更新时打开文件

标签 c# entity-framework sqlite

我对这个错误完全感到困惑。我曾经在从数据库文件读取时遇到无法打开文件错误。我发现我遇到了文件路径问题并修复了它。现在当我尝试更新数据库时我得到了它。阅读后会发生这种情况。为什么更新时无法打开,而读取时却无法打开?

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: 

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>

最佳答案

请为 Application Pool 用户授予 SQLite 文件夹对 App_Data 目录的写入权限。

Change App_Data/SQlite folder in plesk

关于c# - Sqlite Entity Framework 无法在更新时打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30484662/

相关文章:

c# - 将 cookie 从 CookieContainer 写入 IE cookie 存储

asp.net - Code First 启用迁移

php - Symfony2 + Doctrine,在关系中找到

c# - 在 EF 的延迟加载上使用自定义 where 子句

python - python 字符串的 SQL 语法错误

sqlite - 使用 SQLite 如何索引 CREATE TABLE 语句中的列?

Android:从 Assets 文件夹复制数据库,但只得到一个空文件

c# - Microsoft.Practices.Composite 是 Microsoft.Practices.Prism 的早期版本吗?

c# - System.Collections.Stack.Peek() 与 FirstOrDefault()

c# - 如何简化对泛型方法的调用,我创建了该方法以跨项目的不同层从一种类型转换为另一种类型