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.
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.
Now let's check the solution explorer. for DBContext and table object. This will get automatically get created after successful execution of the Scaffold.
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.
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.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.