repost: CRUD Operations in WPF using EntityFrameworkCore and SQLite
In this blog post, we will discuss how to perform CRUD operations in WPF using EntityFrameworkCore and SQLite as the backend database. We will create a .NET Core WPF application and all the basic Create, Read, Update, and Delete operations will be performed via the user interface interactions. If you are completely new to .NET Core WPF, I would highly recommend you go through the introductory post of Windows Presentation Foundation in .NET Core.
In this blog post, we are going to create a .NET Core WPF Application using the default template provided by the Visual Studio 2019. The other tools and packages required are as follows:
Visual Studio 2019 (v16.4) download here
.NET Core SDK 3.1 and above [download here(https://dotnet.microsoft.com/download/dotnet-core/3.1)]
We are going to create a straight forward .NET Core WPF application, that reads all the records from the Products table inside a Product.db, SQLite database. These product details are displayed inside a DataGrid on the user interface.
An individual Edit button is displayed on every row of the DataGrid, clicking the same will update the selected product details on the “Edit product”, from where the user can change any value of the product and update the same.
Likewise, an individual Delete button is displayed in every row of the DataGrid, clicking the same deletes the record from the Product table.
The application also contains a section from where the user can add (create) a new product in the database.
The first step is to create a .NET Core WPF Application in Visual Studio 2019, this quick video display how to create the WPF application in Visual Studio 2019.
ProductDbContext.cs: This is a DbContext class that helps us to interact and perform database operations. The class also overrides the OnModelCreating() so that the database can have some seed data for testing purposes.
#region Private methods private Product[] GetProducts() { returnnew Product[] { new Product { Id = 1, Name = "TShirt", Description = "Blue Color", Price = 2.99, Unit =1}, new Product { Id = 2, Name = "Shirt", Description = "Formal Shirt", Price = 12.99, Unit =1}, new Product { Id = 3, Name = "Socks", Description = "Wollen", Price = 5.00, Unit =2}, new Product { Id = 4, Name = "Tshirt", Description = "Red", Price = 2.99, Unit =3}, }; } #endregion }
# Step 4: Register the ProductDbContext and MainWindow class in ServiceProvider in App.xaml.cs
Here in this class, we are making use of Dependency Injection (DI), and registering the ProductDbContext and MainWindow with the DI’s ServiceProvider. As well as, we have created a handler for the Startup event where we are displaying the MainWindow after getting the instance from the ServiceProvider.
<LabelFontSize="18"HorizontalAlignment="Center"VerticalAlignment="Center"Margin="5" Grid.Row="0"Content="CRUD Application using EntityFrameworkCore and SQLite"/>
privatevoidSelectProductToEdit(object s, RoutedEventArgs e) { selectedProduct = (s as FrameworkElement).DataContext as Product; UpdateProductGrid.DataContext = selectedProduct; }
privatevoidDeleteProduct(object s, RoutedEventArgs e) { var productToDelete = (s as FrameworkElement).DataContext as Product; context.Products.Remove(productToDelete); context.SaveChanges(); GetProducts(); } }
After executing the application, the Product.db will be created in the binary directory of the application. The records of the Product table when the application creates the database for the first time.