In this article, we will see how to create a .NET project with Entity Framework to connect to a database.
Logically, this is the first step if you want to use EF in your applications. Fortunately, it’s a very simple process.
We can add Entity Framework to any type of .NET program, such as console applications, desktop applications, ASP.NET (or any other type of application).
In this tutorial, we will go through the process step by step, using a console application as an example. But for other types of projects, it’s basically identical.
Prerequisites
Before installing and configuring Entity Framework, make sure you have the following installed on your system:
- .NET SDK: It is the foundation for developing applications in .NET.
- An IDE or code editor: I recommend using Visual Studio 2022 or Visual Studio Code.
- Basic knowledge of C#: Because EF is a C# tool.
- A database management system. Depending on the database you want to choose.
If you’re missing any of these, take a look at,
Creating a Console Project with .NET
To create a console project in .NET, open a terminal or command prompt and run the following command:
dotnet new console -n MyEFProject
This command will create a new folder called MyEFProject
with a basic console project.
Once the project is created, navigate to the project directory:
cd MyEFProject
Adding Entity Framework Core
EF Core is available as a collection of NuGet packages. We will need to install the following essential packages:
EntityFrameworkCore: This package contains the core of EF Core and is necessary for any project:
dotnet add package Microsoft.EntityFrameworkCore
Database Provider: EF Core needs a specific provider depending on the database you use. For SQL Server:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
EF Core Tools: This package allows the use of command-line tools for migrations and other tasks:
dotnet add package Microsoft.EntityFrameworkCore.Tools
Creating Entities
Entities represent the tables in the database in your code. For this example, we will create a class called User
.
To do this, within the Models
folder, create a new class named User.cs
:
namespace MyEFProject.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
}
This class represents a User
entity with three properties: Id
, Name
, and Email
.
Configuring the Database Context
Entity Framework uses a DbContext object to interact with the database. We will create a class that inherits from DbContext
to configure our context.
In the root folder of the project, create a class named AppDbContext.cs
with the following content:
using Microsoft.EntityFrameworkCore;
namespace MyEFProject.Models
{
public class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost;Database=MyDatabase;Trusted_Connection=True;");
}
}
}
In this example,
- We created a
DbContext
calledAppDbContext
that contains aDbSet
for theUser
entity. - Additionally, we configured the connection to a local SQL Server database.
Creating the Database
Entity Framework uses migrations to apply model changes to the database. To create a migration, run the following commands:
Generate an initial migration:
dotnet ef migrations add InitialCreate
Apply the migration to create the database:
dotnet ef database update
Verify that the database has been created in SQL Server Management Studio (SSMS).
Running the Project
Finally, run the project to ensure everything works correctly:
dotnet run
If everything is set up correctly, you should see a message in the console indicating that the project has run without errors.