SQLite实体框架无法在更新时打开文件 - c#

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

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>

参考方案

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

SQLite实体框架无法在更新时打开文件 - c#

System.Data.SQLite-无法访问加密的数据库 - c#

我在DB Browser for SQLite中为数据库设置了加密密钥,现在无法在C#中访问它。以下是相关代码: private SQLiteConnection connection; public DbManager() { connection = new SQLiteConnection("Data Source=DB\\gamedb.en…

更新System.Data.SQLite.dll后,SQLite无效URI - c#

我首先有以下SQLite版本:1.0.77.0(sqlite-netFx40-static-binary-bundle-Win32-2010-1.0.77.0)而且一切正常。将System.Data.SQLite.dll更新到版本1.0.82.0(sqlite-netFx40-static-binary-bundle-Win32-2010-1.0.82.0)…

SQLIte SUM日期时间 - c#

我有一个包含行和时间值的数据库。我想要该时间值的总和(通常,格式为%H:%M)。我这样使用SUM值:SELECT SUM(n_hores) FROM table; 但只有我得到0.0。我该怎么做?谢谢 参考方案 hh:mm格式的时间值为字符串,因此不能对字符串进行有意义的求和。您应该将这些时间值转换为数字(strftime(%s)返回自1970-01-01以…

C#无法将类型为“ System.Double”的对象转换为类型为“ System.Single” - c#

在判断此问题已得到回答之前,请阅读说明。我在下面有这个简单的代码:Dictionary<string, object> d = new Dictionary<string, object>(); d.Add("key" , 30d); System.Diagnostics.Debug.WriteLine($…

如何正确增加喜欢和不喜欢的人 - javascript

我在每个帖子上都有一个按钮1)点赞按钮如果用户已经喜欢了该帖子,则显示“与众不同”按钮如果用户不喜欢该帖子,则为HTML部分<a href="javascript:void(0);" id="liker" data-count="0" data-fpc="481" data…