EF Core 101: A Beginner's Guide to Entity Framework Core

Entity Framework Core, also known as EF Core, is an open-source, lightweight, and cross-platform version of the Entity Framework. EF Core is a powerful object-relational mapper (ORM) that enables .NET developers to work with databases using domain-specific objects. EF Core supports a variety of database providers and is designed to work with many different types of databases, including SQL Server, SQLite, MySQL, PostgreSQL, and others.

In this article, we'll provide a beginner's guide to Entity Framework Core, outlining its key features and explaining how to get started with it.

Installing EF Core

EF Core can be installed using the .NET Core CLI or Visual Studio Package Manager Console. To install EF Core using the .NET Core CLI, run the following command:

dotnet add package Microsoft.EntityFrameworkCore

Once you've installed EF Core, you can begin using it in your application.

Creating a DbContext

The DbContext is the main class that is used to interact with a database using EF Core. It is responsible for creating a connection to the database and for mapping domain-specific objects to database tables. To create a DbContext, you'll need to create a new class that inherits from the DbContext base class, like this:

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }

    public DbSet<Customer> Customers { get; set; }
    public DbSet<Order> Orders { get; set; }
}

In this example, we're defining a DbContext for a fictional application that manages customers and orders. The DbContext has two DbSet properties, one for customers and one for orders. These properties represent database tables and will be used to query and modify data.

Defining Entities

EF Core maps domain-specific objects to database tables using entity classes. An entity class is a simple POCO (plain old CLR object) that represents a single row in a database table. To define an entity class, you'll need to create a new class that has properties that correspond to the columns in the database table.

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

In this example, we're defining an entity class for customers. The class has three properties, Id, Name, and Email, which correspond to the columns in the customers table.

Querying Data

Once you've defined a DbContext and entities, you can use EF Core to query data from the database. EF Core provides a rich set of querying APIs that allow you to filter, sort, and project data. Here's an example of how to query all customers from the database:

using (var context = new MyDbContext(options))
{
    var customers = context.Customers.ToList();
}

In this example, we're creating a new instance of the MyDbContext class and using the Customers DbSet property to query all customers from the database. The ToList method executes the query and returns a list of Customer objects.

Modifying Data

In addition to querying data, EF Core also allows you to modify data in the database. You can add, update, and delete entities using the DbContext. Here's an example of how to add a new customer to the database:

using (var context = new MyDbContext(options))
{
    var customer = new Customer { Name = "John Doe", Email = "johndoe@example.com" };
    context.Customers.Add(customer);
    context.SaveChanges();
}

In this example, we're creating a new instance of the Customer class and adding it to the Customers DbSet property of the MyDbContext class.

Related posts

Add comment

Loading