December 11, 2014 by Anuraj
.Net ASP.Net ASP.Net MVC CodeProject EF Code First Entity Framework
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 :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub