SQLite Error 14: 'unable to open database file' with EF Core code first

26,103

Solution 1

i think the issue is that the EntityFramework Core can't create folders by itself while using SQLite provider. Don't know if the issue also appears when using other filebased database providers.

i had the same issue:
my datasource was something like: optionsBuilder.UseSqlite(@"Data Source=c:\foo_db\bar_db.db");

after i created the "foo_db" folder inside the c:\ drive, the problem was solved and EF Core created the .db file inside the folder.

Solution 2

Solved it.

  1. Activating "break on all exceptions" (in exceptions settings window) caused the weird 'unable to open database file' exception.

  2. Removing the [Table("TableName")] attributes on my entity classes caused some strange table creation behavior in the migration class. I thought the attribute is only needed to create a table with another name than the class name.

Solution 3

I also solved the problem by replacing "InProcess" in the project file (*.csproj) with "OutOfProcess". I hope it helps you too.

Solution 4

The Actual issue is after migration and database update *.db file doesn't go to bin folder automatically. You Just need to select the *.db and change properties "Copy to Output Directory" = "Copy if newer". This will resolve the issue.. please try and let us know.

Solution 5

i Was in the Same Situation my problem was fix by Moving down Down like the pic :

enter image description here firts "AllowedHosts": "*", and them

"ConnectionStrings": {
    "DefaultConnection": "DbExample"
  }

thats happend to me with the Version NetCore 2.1 i hope this can help you out :)

Share:
26,103

Related videos on Youtube

IngoB
Author by

IngoB

Updated on April 26, 2022

Comments

  • IngoB
    IngoB about 2 years

    I am getting an

    SQLite Error 14: 'unable to open database file'

    with EF Core code first, no idea why. I worked fine the first time, the database file got created in c:\users\username\AppData\Local\Packages\PackageId\LocalState.

    Then I deleted the database file and the code first migration and ModelSnapshot classes and created a new migration (I am calling DbContext.Database.Migrate() on app start to automatically execute them). Now the database cannot be created again.

  • Smit
    Smit almost 7 years
    I believe #2 is not related to it. It is not an issue (or error either). You deleted database. When you call Migrate (or even EnsureCreated), EF needs determine if the database already exists or not (so that it can create one if not existing). EF does that by connecting to database. If it is successful then database exists if it fails then it doesn't. Since you have break on all exceptions, it broke the code when you got exception while connecting to database. If you continue the execution then EF will catch the exception and create database for you.
  • tgarcia
    tgarcia almost 5 years
    For those who want to understand how this works, basically InProcess means it will be running inside a IIS instance (alongside a bunch of configs) and OutOfProcess runs Kestrel. Here's the source docs.microsoft.com/en-us/aspnet/core/host-and-deploy/… .
  • lwb
    lwb about 2 years
    This saved me hours! Thanks! For whatever reason EF cannot create folders. Only the db file