0

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.

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I noticed that during the publish process the web.config takes the same values as I have in another connection. See DbProvidersFactory above. When I manually copy the file it works just fine.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.