ratanmalko
ratanmalko

Reputation: 471

ASP.net MVC - sqlite access on IIS server "unable to open database file"

In my asp.net MVC application I access a sqlite DB (ready-only) that I placed in the App_Data folder.

   connection string='data source=|DataDirectory|tutorials.db'

Update:

Complete connection string:

<add name="myEntities" connectionString="metadata=res://*/Models.MyData.csdl|res://*/Models.MyData.ssdl|res://*/Models.MyData.msl;provider=System.Data.SQLite.EF6;provider connection string='data source=|DataDirectory|myDB.db'" providerName="System.Data.EntityClient" />

Under localhost this works just fine. But when I publish the application to an IIS server I get the following error:

unable to open database file

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SQLite.SQLiteException: unable to open database file

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[SQLiteException (0xe): unable to open database file]
   System.Data.SQLite.SQLite3.Open(String strFilename, String vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool) +627
   System.Data.SQLite.SQLiteConnection.Open() +5031
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c) +10
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +72
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) +359
   System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2() +55
   System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Action operation) +9
   System.Data.Entity.Core.EntityClient.EntityConnection.Open() +253

[EntityException: The underlying provider failed on Open.]
...

DbContext:

public partial class myEntities : DbContext
{
    public myEntities()
        : base("name=myEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<MyData> myData { get; set; }
}

Update:

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

I am not sure if the connection string or something else is the problem. I did however change the websites main directory within IIS.

I can load other files from within the App_Data folder just fine.

How can I fix this?

Thank you.

Upvotes: 0

Views: 2518

Answers (3)

ahmed
ahmed

Reputation: 1

This is my solution:

  1. I put my sqlite file in the wwwroot folder

  2. I change my connection string in appsttings.json file to be

    "ConnectionStrings": {
      "defualtConnection": "data source= wwwroot/newDataBase.db"
    }
    

Upvotes: 0

ratanmalko
ratanmalko

Reputation: 471

I figured it out. The connection string somehow was saved under a different name in the "Publish Web" GUI in the Settings tab.

Upvotes: 0

Linh Tuan
Linh Tuan

Reputation: 448

The connection string for SQLite only requires the name of the database file set in the Data Source parameter of the connection string. So your connection strings section could look something like this:

<connectionStrings><add name="YourConnection"
     connectionString="data source=|DataDirectory|YourDB.sqlite"
     providerName="System.Data.SqlLite"/></connectionStrings>

Upvotes: 3

Related Questions