1

I am completely baffled by this error. I used to get the unable to open file error when I was reading from the database file. I figured out I had a file path issue and fixed it. Now I get it when I try and update the database. This happens after a read. Why would it fail to open on an update but not a read?

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();
    }
}
...

error looks like this:

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 config:

<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>
jensengar
  • 6,117
  • 17
  • 58
  • 90

2 Answers2

2

Please give write permission to App_Data directory for Application Pool user for SQLite folder.

Change App_Data/SQlite folder in plesk

Amir Astaneh
  • 2,152
  • 1
  • 20
  • 20
  • 1
    Worked for me. I had granted `Modify` and `Write` on the `.db` file, but I didn't realize the parent directory needed them too. – Eric Eskildsen Apr 25 '19 at 20:20
0

SQLite can only handle one write at a time but can handle multiple simultaneous reads

refer this

Community
  • 1
  • 1
JOW
  • 244
  • 4
  • 18
  • I am doing 1 read and then trying to do 1 insert and I get a cannot open file error. It doesn't say anything about it being locked though. But either way, I am only doing 1 write so I shouldn't have locking issues anyway right? – jensengar May 27 '15 at 14:35
  • yeah, if you are doing one write then you should be fine. by the way, if you are opening a new connection for your insert statement, be sure to have the right file path there also. i mean if you have `SQLiteConnection m_dbConnection; m_dbConnection = new SQLiteConnection("Data Source=myDB.db;Version=3;"); m_dbConnection.Open();` statements written in more than one place, be sure to get the path right in all the places – JOW May 27 '15 at 14:55
  • Well it should support multiple connections right? Just now multiple write ops. – jensengar May 27 '15 at 15:05
  • 1
    maybe this [link](https://www.sqlite.org/faq.html#q5) might answer your last comment – JOW May 27 '15 at 15:16