WinForm
Last updated
Last updated
Improve EF Core performance with EF Extensions
A Windows Forms application is an event-driven application supported by Microsoft's .NET Framework. Unlike a batch program, it spends most of its time simply waiting for the user to do something, such as fill in a text box or click a button.
Windows Forms provides access to native Windows User Interface Common Controls by wrapping the existent Windows API in managed code.
With the help of Windows Forms, the .NET Framework provides a more comprehensive abstraction above the Win32 API than Visual Basic or MFC did.
The project type comes with all the template files you will need before adding anything. Let's open Visual Studio 2019, if you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
On the start window, choose Create a new project.
On the Create a new project window, enter or type windows forms in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list. Select the Windows Forms App (.NET) template, and then choose Next.
In the Configure your new project window, type or enter EFCore5InWinFormApp in the Project name box and click on the Create button, and you will see Visual Studio opens your new project.
To use Entity Framework Core we need to install Microsoft.EntityFrameworkCore library. It is available as a nuget package, and you can install it using Nuget Package Manager.
In the Package Manager Console window, enter the following command.
For SQL Server LocalDB, which is installed with Visual Studio, we need to install Microsoft.EntityFrameworkCore.SqlServer and will get all the packages required for EF Core.
In Solution Explorer, right-click on your project and choose Add > Class. Enter a class file name Author.cs and add the following code.
So let's create a new class file named BookStore.cs, and replace the following code.
We need to add DataGridView to display the data from the database, so let's select the DataGridView from the Toolbox and drag it to the Form1 as shown below.
Now in the Form1_Load
method, we will add some authors' data to the database first, and then we will retrieve the data and display it on the DataGridView as shown below.
If you run the application, you will see that authors are successfully inserted into the database and display on the DataGridView.