Entity Framework Core Interview

EF Core Fundamentals & DbContext

Entity Framework Core interview questions covering fundamentals, ORM concepts, DbContext, DbSet, configuration, conventions, migrations and basic tracking.

25 Questions Filter by level

Interview questions

Open a question for the full answer, code and interview guidance.

25 Results
1
JuniorBeginner

What is Entity Framework Core (EF Core)?

Entity Framework Core is Microsoft's modern object-relational mapper (ORM) for .NET. It allows .NET applications to work with relational databases primarily through .NET objects, LINQ queries, and a DbContext instead of manually writing all SQL and data-mapping code.

2
JuniorBeginner

What is an ORM and why is it useful?

An ORM, or Object-Relational Mapper, maps application objects to relational database structures. It reduces repetitive code for reading rows, converting them into objects, tracking changes, and generating INSERT, UPDATE, DELETE, and SELECT operations.

3
JuniorBeginner

What is DbContext in EF Core?

DbContext is the central EF Core class that represents a session with the database. It exposes entity sets, tracks entity changes, translates LINQ queries, coordinates database operations, and saves changes.

4
JuniorBeginner

What is DbSet<TEntity>?

DbSet<TEntity> represents a collection of entities of a particular type that EF Core can query and persist. It is commonly exposed as a property on DbContext.

5
JuniorBeginner

How do you register EF Core DbContext in ASP.NET Core?

The usual approach is to register the context with AddDbContext in Program.cs and configure a database provider and connection string.

6
JuniorBeginner

What is DbContextOptions?

DbContextOptions contains configuration used to construct a DbContext. It can describe the database provider, connection information, logging behavior, interceptors, query settings, and other EF Core options.

7
JuniorBeginner

What is an entity in EF Core?

An entity is a .NET object type that EF Core includes in its model and can map to database data. A typical entity has scalar properties, a primary key, and optional navigation properties describing relationships.

8
JuniorBeginner

How does EF Core determine the primary key of an entity?

By convention, EF Core recognizes a property named Id or <EntityName>Id as the primary key for many common entity types.

9
JuniorBeginner

What are conventions in EF Core?

Conventions are default rules EF Core uses to build its model when explicit configuration is not provided. They cover areas such as primary keys, relationships, property mapping, and table/entity discovery.

10
JuniorBeginner

What are data annotations in EF Core?

Data annotations are attributes placed on entity classes or properties to configure validation or aspects of EF Core mapping. Common examples include Key, Required, MaxLength, Column, Table, and NotMapped.

11
JuniorBeginner

What is the Fluent API in EF Core?

The Fluent API configures the EF Core model through ModelBuilder, usually inside OnModelCreating or IEntityTypeConfiguration classes.

12
JuniorBeginner

What is OnModelCreating?

OnModelCreating is a DbContext method that can be overridden to configure the EF Core model with ModelBuilder.

13
JuniorBeginner

What is IEntityTypeConfiguration<TEntity>?

IEntityTypeConfiguration<TEntity> allows the mapping for one entity type to be placed in a dedicated configuration class.

14
JuniorBeginner

What is a migration in EF Core?

A migration is a versioned description of changes needed to move a database schema from one model state to another.

15
JuniorBeginner

What is the difference between Add-Migration and Update-Database?

Add-Migration compares the current EF Core model with the previous model snapshot and creates migration code describing the detected schema changes. It does not normally apply those changes to the database.

16
JuniorBeginner

What is the EF Core model snapshot?

The model snapshot is a C# representation of the model state associated with the latest migration. EF Core uses it when generating a new migration to determine what changed.

17
JuniorBeginner

What is change tracking in EF Core?

Change tracking is the mechanism EF Core uses to remember entity instances and their states so it can determine what database commands are required when SaveChanges is called.

18
JuniorBeginner

What are the main EntityState values in EF Core?

EntityState describes how a tracked entity relates to the database.

19
JuniorBeginner

What does SaveChanges or SaveChangesAsync do?

SaveChanges examines tracked entity states and sends the required INSERT, UPDATE, and DELETE commands to the database. If successful, EF Core updates tracking state to reflect the persisted changes.

20
JuniorBeginner

What is the difference between Add, Attach, and Update in EF Core?

Add begins tracking an entity as Added, so EF Core normally inserts it when SaveChanges is called.

21
JuniorBeginner

What is AsNoTracking in EF Core?

AsNoTracking tells EF Core not to place query results into the change tracker. It is useful for read-only queries where the returned entities will not be modified and saved through the same context.

22
JuniorBeginner

What is LINQ in EF Core?

LINQ allows developers to express database queries using strongly typed C# expressions. EF Core examines the expression tree and translates supported parts into SQL for the configured relational provider.

23
JuniorBeginner

What is deferred execution in EF Core LINQ queries?

Many LINQ operators build an IQueryable expression without immediately sending a query to the database. The SQL is executed when the query is materialized or otherwise enumerated.

24
JuniorBeginner

What is the difference between IQueryable and IEnumerable when using EF Core?

IQueryable represents a query expression that a provider such as EF Core can translate and execute against the database. Filtering, projection, ordering, and pagination added while the query remains IQueryable can therefore be translated into SQL.

25
JuniorBeginner

What are good DbContext and query practices for an ASP.NET Core application?

Keep DbContext short-lived and normally scoped to one request or unit of work. Do not share one context concurrently between threads, and do not store it in a singleton service.