crear-proyecto-con-entity-framework

Create a .NET Project with Entity Framework

  • 4 min

In this article, we are going to see how to create a .NET project with Entity Framework to connect to a database.

Logically, it is the first step if you want to use EF in your applications. Fortunately, it is 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 see the process step by step, using a console application as an example. But in other types of projects, it is basically identical.

Prerequisites

Before installing and configuring Entity Framework, make sure you have the following installed on your system:

  1. .NET SDK: It is the foundation for developing .NET applications.
  2. An IDE or code editor: I recommend using Visual Studio 2022 or Visual Studio Code.
  3. Basic knowledge of C#: Because EF is a C# tool
  4. A database manager. Depending on the database you want to choose.

Install the Dotnet-EF Tool

To work with Entity Framework projects, we will need the .NET CLI tool for Entity Framework. To install it, we do:

dotnet tool install —global dotnet-ef

We can verify if it is correctly installed with:

dotnet ef —version

Or update it by doing:

dotnet tool update —global dotnet-ef

Creating a .NET Console Project

To create a .NET console project, open a terminal or command prompt and run the following command:

dotnet new console -n MiProyectoEF

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 MiProyectoEF

Add 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

Create an Entity

Entities represent the database tables in your code. For this example, we will create a class called User.

To do this, inside the Models folder, create a new class called User.cs:

namespace MyEFProject.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }
}
Copied!

This class represents a User entity with three properties: Id, Name, and Email.

Configure the Database Context

Entity Framework uses a DbContext object to interact with the database. Let’s create a class that inherits from DbContext to configure our context.

In the project’s root folder, create a class called 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;");
        }
    }
}
Copied!

In this example,

  • We have created a DbContext called AppDbContext that contains a DbSet for the User entity.
  • Additionally, we have configured the connection to a local SQL Server database.

Create 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)

Run the Project

Finally, run the project to make sure everything is working correctly:

dotnet run

If everything is configured correctly, you should see a message in the console indicating that the project has run without errors.