Console .Net 6.0 App with Entity Framework Core 6.0.6

In this article, we will create a pretty simple  Console Application .net 6.0 that connects to the MS SQL Database using the Entity Framework Core 6.0.6. The main purpose of this application is to check what packages we need and how to access the database.  We will connect to the ‘YarkulTestDb1’ database and print Person First Name and Last Name in the console.

1. Create a new Project

The first step is to create a console application. It is important to choose .net 6.0 Framework.

Create new project Console Application
(click to open in a new tab)


Console Appliation .net Framework 6.0
(click to open in a new tab)

2. Install Nuget Packages

You need to install these packages

  • Microsoft.EntityFrameworkCore 6.0.6
  • Microsoft.EntityFrameworkCore.SqlServer 6.0.6

The first package is required for the EntitryFrameworkCore support and the second requires for SQL Server use, in particular, to use the ‘UseSqlServer(connectionString)’ function.


nuget packages
(click to open in a new tab)

2. Database Structure

For test purposes, we will use a simple database with one table.


(click to open in a new tab)

Create Db Context and Entity Class

To access our database we need to create at least two files. The first one is YarkulDbContext.cs and the second is Person.cs.

Declare DbContext class

The YarkulDbContext.cs is nested from the DBContext. This class has only one constructor with the DbContextOptions parameter. It requires to setup our DB settings, especially the connection string. Also, you can see the DbSet Persons property. As you can see this property fully reflect our dbo.Person table.

using Microsoft.EntityFrameworkCore;
namespace ConsoleApp1
{
    public class YarkulDbContext : DbContext
    {
        public YarkulDbContext(DbContextOptions<YarkulDbContext> options)
                : base(options) { }
        public DbSet<Person> Persons { get; set; }
    }
}

Declare Entity Class – Person

As was mentioned above this class fully reflects our table in the YarkulTestDb1 database. Using the [Table(name:”Person”)] attribute we specified the table name and using the [Key] attribute we marked our ‘Id’ field as the db key. Other properties do not require attributes. By default they map by name – the property name should be the same as a column name.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ConsoleApp1
{
    [Table(name:"Person")]
    public class Person
    {
        [Key]
        public int Id { get; set; }
        public string? FirstName { get;set; }
        public string? LastName { get; set; }
    }
}

Connect to the Db – Main Code

After we have DbConext and Entity classes let’s create a connection and read all rows from the dbo.Person table. Using the DbContextOptionsBuilder class we create an options file. The UseSqlServer function take one parameter – connection string. Usually, the connection string is stored in the appSettings.json file. But to simplify the application we pass this parameter directly to the parameter. Read here how to get the connection string from the appSettings.json file.

using ConsoleApp1;
using Microsoft.EntityFrameworkCore;
var yarkulDbOptions = new DbContextOptionsBuilder<YarkulDbContext>()
    .UseSqlServer("Server=YKULISH-NB\\YARKULSQLEXPRESS;initial catalog=YarkulTestDb1;" +
    "Integrated Security=true;MultipleActiveResultSets=true;App=ConsoleApp1")
    .Options;
using (var yarkulDbContext = new YarkulDbContext(yarkulDbOptions))
{
    var persons = yarkulDbContext.Persons.ToList();
    foreach (var person in persons)
    {
        Console.WriteLine($"Hello {person.FirstName} {person.LastName}");
    }
}

The ‘using’ statement is required because the ‘YarkulDbContext'(DbContext) file is nested from the IDisposable interface. The ‘persons’ variable contains all our rows from the table. Usually, we need to filter data but again our application is simple and we know there are only two rows. Read here how to enable SQL logging and check what SQL statements are generated by Entity Framework.

Print Persons to the Console

Here is the result of the program execution:

get persons from the table and print in console

As you can see there is no magic to connect your application with the MS SQL Database.
Used Artifacts:

  • .Net 6.0 Console Application
  • Entity Framework Core 6.0.6
  • Visual Studio 2022
  • MS SQL Server 2019

You can download this application here(github).

Leave a Reply

Your email address will not be published.