Posted by Anuraj on Thursday, December 11, 2014
SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. Most of the ASP.NET 5 tutorials / articles, SQL Server is used. In this post I am explaining about how to use SQLite with Entity Framework 7.0.
First you need to modify the project.json file, and need to add SQLite and Entity Framework dependencies. (“EntityFramework.SQLite”: “7.0.0-*”),the project.json, dependencies section will look like this.
Now you need to create DbContext class. If you are using SQL Server, you can generate it using scaffolding(For SQLite, it did worked for me.) Here is minimal DbContext class and the model class I have created.
As you are using EF code first (creating the DB on the fly), you need to add the following code in the constructor of the Startup.cs, which will ensure SQLite Db is created, it is similar to Database Initializer classes in Entity Framework.
This method won’t throw exception even if DB exists. Now you can write simple EF insert statement to insert data to the Table.
In the next post I will try explain how to scaffold controllers and views in ASP.NET 5 with SQLite.
Happy Programming :)