Introducing Entity Framework Core
Entity Framework Core (EF Core) is an object-relational mapping (ORM) framework for .NET. It allows developers to work with databases using .NET objects, and provides a way to interact with databases in an object-oriented way rather than using raw SQL.
Why use EF Core?
- Code-First Development: EF Core allows developers to define their database schema using .NET classes and then automatically generate the corresponding database tables. This approach, known as Code-First development, promotes a more agile and flexible approach to database design.
- Object-Relational Mapping (ORM): EF Core maps .NET objects to database tables, making it easier to work with databases in an object-oriented manner. This mapping is handled by the Entity Framework Core ORM, which simplifies the process of interacting with databases.
- Query Abstraction: EF Core provides a powerful and flexible query abstraction layer that allows developers to write database queries in a more expressive and readable way. This layer abstracts the underlying SQL syntax, providing a more intuitive and consistent way to interact with databases.
- Change Tracking: EF Core includes built-in change tracking mechanisms that help manage changes to data in memory and synchronize them with the database. This feature simplifies the process of updating and deleting data, reducing the risk of data inconsistencies and ensuring data integrity.
- Database Migrations: EF Core supports database migrations, allowing developers to modify database schemas incrementally. This feature simplifies the process of updating databases in a controlled and version-controlled manner, making it easier to manage database changes over time.
How does EF Core work in code?
DbContext
: TheDbContext
is the main class in EF Core that represents a session with the database. It manages the connection to the database, the creation of database queries, and the execution of database commands.DbSet
: TheDbSet
is a collection property in theDbContext
that represents a collection of entities in the database. It provides methods for querying and manipulating the corresponding database table.
Here's what the code looks like:
public class MyDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
The DbSet
is a collection of entities that are mapped to a database table. It provides methods for querying and manipulating the corresponding database table.
You can query a DbSet
using LINQ, and it will be translated to a SQL query and executed against the database.
var employees = context
.Employees
.Where(e => e.Department == "Sales")
.ToList();
Under the hood, this roughly translate to the following SQL:
SELECT * FROM Employees
WHERE Department = 'Sales';
How to install EF Core
You can install EF Core using the NuGet Package Manager.
dotnet add package Microsoft.EntityFrameworkCore
There are a ton of database providers available for EF Core, including:
- SQL Server
- SQLite
- PostgreSQL
- MySQL
We're going to use the SQLite database provider in this course because it's super easy to get started with.