Entity Framework Core
Entity Framework Core (EF Core) is a modern object-relational mapper (ORM) for .NET. It enables .NET developers to work with a database using domain-specific objects that are generally entity framework concepts, and it minimizes the amount of data-access code that the developer needs to write. EF Core is a lightweight, extensible, and high-performance version of the popular Entity Framework. It is cross-platform and can be used with various .NET applications, including ASP.NET Core, Windows, macOS, and Linux.
Key Features
- LINQ Queries: Write database queries using Language Integrated Query (LINQ).
- Migrations: Manage database schema changes over time.
- Change Tracking: EF Core automatically tracks changes made to entities.
- Connection Resiliency: Automatically retries failed database commands.
- Cross-Platform: Works on Windows, macOS, and Linux.
- Performance: Optimized for performance and efficiency.
- Extensibility: Customizable through interceptors, conventions, and more.
Getting Started
To get started with EF Core, you typically need to install the appropriate NuGet packages for your database provider.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Then, you define your data model using C# classes and create a derived DbContext
instance to represent a session with the database and allow you to query and save data.
Core Concepts
- DbContext: The primary class used to interact with the database.
- DbSet
: Represents a collection of a given entity type in the context. - Entities: Plain old CLR objects (POCOs) that represent data in the database.
- Value Objects: Objects that represent a value, not an identity.
- Mappings: How your .NET classes and properties map to database tables and columns.
Common Scenarios
- Creating a new database: Using EF Core Migrations to create your database schema from your model.
- Querying data: Fetching data from the database using LINQ.
- Adding, Updating, and Deleting data: Modifying entities and saving changes to the database.
- Relationships: Defining and working with one-to-one, one-to-many, and many-to-many relationships.
- Advanced Topics: Including raw SQL queries, stored procedures, and custom conventions.
Explore the following resources for more in-depth information and tutorials.