Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 845
IngoB Avatar asked May 28 '17 21:05

IngoB


2 Answers

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.

like image 106
darkdog Avatar answered Oct 16 '22 19:10

darkdog


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.

like image 6
IngoB Avatar answered Oct 16 '22 19:10

IngoB