How to Connect SQLite Database In .Net Core Using Entity Framework, C#

How to connect to connect to SQLite database and get data from table in .net core using entity framework in c#.net. Scaffold to get table object.
In today's article I will show you a simple tutorial with and example how you can connect to SQLite database using Entity Framework in windows application (.net core) using c#.net and get records. So, to connect to SQLite database in your windows application using c#.net. First, we will create a new SQLite database by using SQLite DB Browser or by using SQLite Studio. After creating the database, we will adda table and add few records in it.

SQLite Employee Table

So here we are having three records in our SQLite database.  Now we will create a new windows application, and we will install three NuGet packages from NuGet manager. 

SQLite NuGet Package

After installing the above-mentioned packages. We will use scaffold command and as mentioned below.

Scaffold Command Scaffold-DbContext "Data Source=C:\DB\EmployeeDB.db;" Microsoft.EntityFrameworkCore.Sqlite -OutputDir data
The above Scaffold we need to run in the Package Manager. Before executing let's discuss about the above command. Here in Scaffold command, we have defied the connection string and after that I have defined the provider for SQLite which is "Microsoft.EntityFrameworkCore.Sqlite". After that I have defined the -OutputDir data. This is the folder where we will get out DBContext and table object. Now let's run the Scaffold in Package Manager. 
Note: Here you can change the path of the SQLite database in Scaffold command, or you can place the database in bin debug folder so will not require to place the full path of the file Scaffold command when you publish the application. 
SQLite Package Manager

Now let's check the solution explorer.  for DBContext and table object. This will get automatically get created after successful execution of the Scaffold.

SQLite DBcontext

Now let's try to get the data from the SQLite database. Before that we will add a button on the form and on click of this button we will try to get the SQLite database table data.

Get Data on button click from  SQLite
Now let's write code to get data from SQLite database. For this we will generate button click event and add the below code.
EmployeeDbContext employeeDbContext = new EmployeeDbContext();
var data = employeeDbContext.EmployeeMasters.ToList();
StringBuilder sb = new StringBuilder();
foreach (var item in data)
{
    sb.Append(string.Format("{0},{1},{2}", item.Name, item.Address, item.Salery));
    sb.AppendLine();
}
MessageBox.Show(sb.ToString()); 
In above code I have first I have created the object of the DBContext class and after that I have accessed the table object.  At the end I have used string builder to make display record. Now let's run the code by putting a breakpoint and check the final output.

SQLite database table data

Here we can see we are having three records same as we were having in SQLite database table.  Now let's press F5 to check the output.

SQLite final output
how-to-connect-sqlite-database-in-net-core-using-entity-framework-c.zip 119KB

Post a Comment