Introduction
In my previous article Simple Way To Create Sqlite Database, I talked about the choice of the database and I focused on the simplest one which is Sqlite. But even if we choose the right one, we still need to work on the design and on its integration in the application side.
The old way of doing things was to use a database driver in your application, define the connexion and then execute the SQL command and queries from your code. When you get the data from the query execution, you need to parse them and then insert them to your objects. And this was a heavy task.
Later in time appeared Object-Relational Mapping. The ORM.
The ORM is the object-relational Mapping Tool. To make it simple, it's a tool that helps you to translate your database tables to a class Model in your application and vice versa. It also provides you with the methods to perform all the CRUD operation
The approach of creating the database then generating the class model is called Database First. The approach of creating the model in your application and then generating the database is called Code First.
In this article, we will focus on the database first part.
Entity Framework
Entity framework is the most commonly used ORM in .NET projects. It doesn't only allow you to access the database from your application. But also it also simplifies your life by giving you advanced possibilities like managing the concurrency and configuring your model. It has also the advantage of the use of the Language Integrated Query LINQ which is a powerful tool to perform your queries and filters.
Prerequisite
Step 1: Nuget packages
- Create a new console project in visual studio.
- Open the commad line: Go to Tools => NuGet Package Manager => Package Manager Console and then you will get the command line displayed in your editor.
![Get Started With Entity Framework Core Using SQLite]()
- Install the required libraries by executing the following commands :
- PM> Install-Package Microsoft.EntityFrameworkCore.Tools
- PM> install-Package Microsoft.EntityFrameworkCore.Sqlite
- Once you installed all packages you should have the following references in your solution
![Get Started With Entity Framework Core Using SQLite]()
Step 2: Model generation
- To generate the model, you need to use also the command line
- You need to copy the needed database to your project folder and choose the right mode of copy to output directory:
![Get Started With Entity Framework Core Using SQLite]()
- Now you can generate your classes using the following command. You need to specify the output path, in our case it will be the folder Models
- Scaffold-DbContext "Data Source=.\Database\products.db" Microsoft.EntityFrameworkCore.Sqlite -OutputDir Models
- if everything is ok, you should get the classes generated under the folder Models and the following message in the package manager console should appear
![Get Started With Entity Framework Core Using SQLite]()
- The warning is there because the generator hardcoded the database's connection string in the code. You need to consider moving it in a protected way to a configuration file. But let's ignore it because it's not the main topic of this article.
Step 3: Check your generated classes
Now you should have the Context that contains the model creation and for each table you will get a class. In our case we have only one entity.
The Context class,
The class Item,
Step 4: Write your cruds
Once you have created your model, you can now start performing operations on it. The following code in program.cs allows to add / update or delete an item.
You can test it and then see the results in the database directly.
It's a simple call to every method of the crud. You can play with it by changing the data and adding more controls.
For example if you call the AddItem twice without modifying it you will get an exception related to the constraint of the primary key
Step 4: And finally check your database
If you remember, we have set the property of the database in the solution explorer to "Copy if newer" so the file products.db is copied to the binary folder.
Open the database and browse your file then you will see the inserted item. Ensure that you left at least one product after testing the add and the delete
![Get Started With Entity Framework Core Using SQLite]()