Getting Started with Entity Framework Core in .NET

4 min read 23 hours ago
Published on Jan 08, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through getting started with Entity Framework Core (EF Core) in .NET. EF Core is a powerful Object-Relational Mapping (ORM) framework that simplifies data access in your applications. Understanding its functionality is essential for modern .NET developers.

Step 1: Setting Up Your Development Environment

To begin using EF Core, you need to set up your development environment.

  • Install .NET SDK: Ensure that you have the .NET SDK installed on your machine. You can download it from the official .NET website.
  • Choose an IDE: Use Visual Studio, Visual Studio Code, or JetBrains Rider for development. Make sure you have the latest version of your preferred IDE.
  • Create a New Project:
    • Open your IDE and create a new project.
    • Select “ASP.NET Core Web Application” or “Console Application” based on your requirements.
    • Choose the appropriate .NET version.

Step 2: Adding Entity Framework Core

Next, you need to add EF Core to your project.

  • Open Terminal/Package Manager Console:

    • For Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console.
    • For Visual Studio Code, open the integrated terminal.
  • Install EF Core Packages:

    • Use the following commands to install the necessary packages:
      dotnet add package Microsoft.EntityFrameworkCore
      dotnet add package Microsoft.EntityFrameworkCore.SqlServer
      dotnet add package Microsoft.EntityFrameworkCore.Tools
      
    • Replace SqlServer with your database provider if you are using a different one (e.g., Npgsql for PostgreSQL).

Step 3: Creating Your Data Model

In this step, you will create a data model that represents your database structure.

  • Define Your Entity Classes:

    • Create a folder named Models.
    • Add a class for each entity in your database. For example, create a Product class:
      public class Product
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public decimal Price { get; set; }
      }
      
  • Create a Database Context:

    • Add a class that inherits from DbContext.
      public class AppDbContext : DbContext
      {
          public DbSet<Product> Products { get; set; }
      
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              optionsBuilder.UseSqlServer("YourConnectionStringHere");
          }
      }
      
    • Replace YourConnectionStringHere with your actual database connection string.

Step 4: Creating and Applying Migrations

Migrations allow you to update your database schema.

  • Add a Migration:

    • Run the following command in the Package Manager Console or terminal:
      dotnet ef migrations add InitialCreate
      
  • Update the Database:

    • Apply the migration to your database using:
      dotnet ef database update
      

Step 5: Performing CRUD Operations

Now that your database is set up, you can perform Create, Read, Update, and Delete operations.

  • Creating a New Record:

    using (var context = new AppDbContext())
    {
        var product = new Product { Name = "Sample Product", Price = 9.99M };
        context.Products.Add(product);
        context.SaveChanges();
    }
    
  • Reading Records:

    using (var context = new AppDbContext())
    {
        var products = context.Products.ToList();
    }
    
  • Updating a Record:

    using (var context = new AppDbContext())
    {
        var product = context.Products.First();
        product.Price = 19.99M;
        context.SaveChanges();
    }
    
  • Deleting a Record:

    using (var context = new AppDbContext())
    {
        var product = context.Products.First();
        context.Products.Remove(product);
        context.SaveChanges();
    }
    

Conclusion

You have now set up Entity Framework Core in your .NET application and learned how to create a data model, apply migrations, and perform basic CRUD operations. As you continue to explore EF Core, consider diving into more advanced topics like relationships, data seeding, and querying.

For further learning, check out additional resources and tutorials on EF Core to deepen your understanding and skills.