Console
Improve EF Core performance with EF Extensions
Console
A Console application is a program designed to be used via a text-only computer interface, such as a text terminal, the command line interface, etc.
A user typically interacts with a console application using only a keyboard and display screen, as opposed to GUI applications, which normally require the use of a mouse or other pointing device.
Many console applications such as command-line interpreters are command-line tools, but numerous text-based user interface (TUI) programs also exist.
Create a Console App
To start, we will create a Console application project. 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 console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list. Select the Console App (.NET Core) template, and then choose Next.
In the Configure your new project window, type or enter EFCore5InConsoleApp in the Project name box and click on the Create button.
Visual Studio opens your new project and includes the default "Hello World" code in your project.
Install Entity Framework Core
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.
Create a Data Model and Database Context
To create a data model for our application, we will start with the following two entities.
The database context class provides the main functionality to coordinate Entity Framework with a given data model. So, let's add a new BookStore
class which will inherit the DbContext
class.
Now, we are done with the required classes and database creation, let's add some authors and book records to the database and then retrieve them as shown below.
If you run the application, you will see that authors and books are successfully inserted into the database and also print on the console window.
References
Last updated