How do I create SQLite database files in .NET core?

With Microsoft.Data.Sqlite it is typically taken care of automatically.

When you attempt to open a database with the simple kind of connection string exemplified in the following code, the database will be created, if it does not exist:

using (var connection = new SqliteConnection("Data Source=hello.db")) {
    connection.Open();  //  <== The database file is created here.

    // more code here that creates tables etc.

}

More specifically, this just creates an empty file, which will then be filled with tables etc. when you create those things inside.

But it all depends on the connection mode, which can be specified in the connection string with the keyword "Mode". The default value of that keyword (i.e. when that keyword is omitted) is "ReadWriteCreate" which means that the database is opened for reading and writing, and that it is created it if it doesn't exist. Other values of the keyword "Mode" do not have that effect.

See Microsoft.Data.Sqlite - Connection strings for more information.


Found the System.Data.SQLite source code. It is just this:

/// <summary>
/// Creates a database file.  This just creates a zero-byte file which SQLite
/// will turn into a database when the file is opened properly.
/// </summary>
/// <param name="databaseFileName">The file to create</param>
static public void CreateFile(string databaseFileName)
{
    File.WriteAllBytes(databaseFileName, new byte[0]);
}