How To Use Entity Framework Core In a Console Application?
.NET Core .NET C#

How To Use Entity Framework Core In a Console Application?

Editorial Team
Editorial Team

Entity framework core is an ORM (Object Relational Mapper) for .NET. It can be configured to work with different databases like SQL Server, MySQL, SQLite, etc through database providers. In this post, we'll learn to use Entity Framework in a .NET Core Console application.

Create a Console Application

Create a new .NET Core console application in Visual Studio. Select .NET 6 or newer as the framework version.

Install Entity Framework

The next step is to install the required packages. This includes Entity Framework Core, MSSQL Database Provider, and Entity Framework Core Tools.

The Tools package is optional. However, if you want to use commands like Add-Migration and Update-Database, this package is required.

To install these packages, open NuGet Package Manager Console and run the following Commands. NuGet Package Manager Console can be opened from Tools -> NuGetPackage Manager -> Package Manager Console.

Install-Package Microsoft.EntityFrameworkCore -Version 6.0.6
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.6
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 6.0.6

Create DB Context

After installing the required packages, we have to create a DBContext. So, create a new class called ApplicationDbContext and add the following code to it. I recommend adding it into a folder called Data. The ApplicationDbContext class should inherit the DbContext class. You have to add a reference to Microsoft.EntityFrameworkCore.

using Microsoft.EntityFrameworkCore;

namespace ConsoleApp3.Data;

internal class ApplicationDbContext: DbContext
{
    
}

The next step is to provide Entity Framework with database configurations and a connection string. For that, we have to override the OnConfiguring method of our ApplicationDbContext class.

Create a Model

As the last step, we have to create a model. In this example, I am creating a  model to save the Name and Value of a color.

Add the model to the database context.

using ConsoleApp3.Models;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApp3.Data;

internal class ApplicationDbContext: DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlServer("Server=.;Database=MyDb;Integrated Security=true");
    }

    public DbSet<Color> Colors { get; set; }
}

Execute Query

Finally, let us execute some queries.

using ConsoleApp3.Data;
using ConsoleApp3.Models;

var db = new ApplicationDbContext();
var colors = new Color[]
{
    new Color(){Name = "Red", Value = "FF0000"},
    new Color(){Name = "Green", Value = "00FF00"},
    new Color(){Name = "Blue", Value = "0000FF"},
};
db.Colors.AddRange(colors);
db.SaveChanges();
ūüĎČ
Starting with .NET 6 (C# 10), we can avoid the explicit type declaration for the arrays and other types.

Conclusion

In this post, we learned to use Entity Framework in a .Net Core Console Application. If you have any questions, let me know in the comments below.



Join the conversation.