Create SQLite DB using Entity Framework Core Code First

In my earlier post, I mentioned that Entity Framework Core supports multiple DB providers. And SQLite is one of them. In this post, we will see how to create SQLite DB using Entity Framework Core code first approach.

Create SQLite DB using Entity Framework Core Code First

For demonstration, let’s create a .NET Core console application.

Entity Framework Core is lightweight and compared to its previous versions. Since EF Core was developed from scratch and it is decomposed in smaller packages so you can use only those which are needed in your project. So open Project.JSON and add highlighted line in the dependencies section to install SQlite nuget package. Save Project.json and wait until VS 2015 installs the package.

Now we need to create our DBContext. Add a new class file and name it SampleDBContext.cs and add following code. As mentioned in post, Database initializers no longer exist in EF Core. There are no such strategies as CreateDatabaseIfNotExists, DropCreateDatabaseIfModelChanges, DropCreateDatabaseAlways, MigrateDatabaseToLatestVersion. There are 2 new methods EnsureCreated() and EnsureDeleted() to create/delete the database. Below code in class constructor shows how to implement DropCreateDatabaseAlways.