Sobes.tech
Junior — Middle

How is data extracted from Entity Framework Core and how does the interaction with the database occur?

sobes.tech AI

Answer from AI

In Entity Framework Core, data retrieval occurs through the database context (DbContext), which represents a session with the database. Data is obtained using LINQ queries to DbSet entities.

Example of data retrieval:

using(var context = new AppDbContext())
{
    var users = context.Users
                       .Where(u => u.IsActive)
                       .ToList();
}

Interaction with the database occurs as follows:

  1. You create a LINQ query to the DbSet.
  2. EF Core translates this query into SQL.
  3. SQL is sent to the database.
  4. Results are returned and materialized into C# objects.

The context tracks entity changes, and when SaveChanges() is called, it synchronizes changes with the database (INSERT, UPDATE, DELETE). Thus, EF Core acts as an ORM, abstracting SQL operations and allowing data manipulation through objects.