Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Entity Framework Core (EF Core) is an object-relational mapper (ORM). An ORM provides a layer between the domain model that you implement in code and a database. EF Core is a data access API that allows you to interact with the database by using .NET plain old Common Runtime Language (CLR) objects (POCOs) and the strongly typed Language Integrated Query (LINQ) syntax.
In EF Core, the database is abstracted behind .NET POCOs. EF Core handles direct interaction with the underlying database. When you use this API, you can spend less time translating requests to and from the database and writing SQL and more time focusing on important business logic.
With EF Core, you can:
The following diagram depicts the architecture of EF Core:
DbContext is a special class that represents a unit of work. DbContext
provides methods that you can use to configure options, connection strings, logging, and the model that's used to map your domain to the database.
Classes that derive from DbContext
:
DbSet<T>
that represent tables in the database.The EF Core Provider translates object graph changes to SQL.
The database provider:
EF Core provides two primary ways of keeping your EF Core model and database schema in sync:
To choose between these options, decide whether your EF Core model or the database schema is the source of truth.
In real-world projects, data models change as app features are implemented. As new entities are added and removed, the database schemas need to be changed accordingly. The EF Core migrations feature provides a way to incrementally update the database schema to keep it in sync with the application's data model while preserving existing data in the database.
When a data model change is introduced, the developer uses EF Core tools to add a corresponding migration. EF Core compares the current model against a snapshot of the previous model to determine the differences. C# code to implement the changes is generated. You can modify the C# files for custom behaviors or to seed data. The files are tracked in your project's source control like any other source file.
After a new migration is generated, it can be applied to a database in various ways. EF Core records all applied migrations in a special history table. The history table keeps a record of which migrations were applied.
Reverse engineering is the process of scaffolding entity model classes and a DbContext
class based on a database schema. This approach is often used with existing or shared databases that a database administrator manages.
In the next unit, you get started with EF Core by using a migration.
Please sign in to use this experience.
Sign in