All questions
Showing of 54What are the main approaches to using Entity Framework Core?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
EF Core supports two main approaches, Code First and Database First. They differ in which side defines the model, your C# classes or an existing database.
With Code First, you write the entity classes and a DbContext first. Migrations then create the database and change its schema as the classes change. This suits new projects where the team has full control over the data model.
With Database First, you start from an existing database. The Scaffold-DbContext command, or dotnet ef dbcontext scaffold on the CLI, generates the DbContext and entity classes from its tables. This suits existing databases and database-driven development, where the schema is designed first.
Model First belongs to EF6, not EF Core. EF Core has no visual designer and no EDMX model file, so you model in code or start from the database.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is an Entity in Entity Framework Core?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
An entity is a C# class that EF Core maps to a database table, and each instance of it is one row in that table.
public class Product
{
public int Id { get; set; } // Primary key
public string Name { get; set; }
public decimal Price { get; set; }
public int CategoryId { get; set; }
// Navigation property
public Category Category { get; set; }
}
By convention, a property named Id or <TypeName>Id becomes the primary key. The other properties map to columns, and Category is a navigation property that points to a related entity.
An entity must be a class, not a struct, and it should have a key. EF Core does not need a parameterless constructor, because it can call a constructor whose parameters match mapped properties. It also maps properties with private setters.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is DbContext and what is its role in Entity Framework Core?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
DbContext is the main class you use to work with the database in EF Core. It represents a session with the database and implements the Unit of Work pattern. It collects your changes and writes them together.
It manages the database connection, translates LINQ queries into SQL, and tracks changes to the entities it manages. Each tracked entity has a state such as Added, Modified or Deleted. When you call SaveChanges, the context writes those changes in a transaction.
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection-string");
}
}
A DbContext is short-lived and not thread-safe, so use one instance per unit of work, such as one web request.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is DbSet and how is it used?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
DbSet<T> represents the collection of all entities of one type, and it usually maps to one table. You declare one per entity type on your DbContext, and it is the starting point for queries and changes.
A LINQ query over a DbSet is translated to SQL and runs when you enumerate it. Add, Update and Remove do not touch the database. They set the entity's state in the change tracker, and SaveChanges writes it. The Local property returns the entities the context already tracks, without a new query.
// Querying
var products = context.Products.Where(p => p.Price > 100).ToList();
// Adding
context.Products.Add(new Product { Name = "New Product" });
// Removing
context.Products.Remove(product);
// Local entities
var localProducts = context.Products.Local.ToList();
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between DbContext.SaveChanges() and DbContext.SaveChangesAsync()?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
SaveChanges() blocks the calling thread until the database finishes, while SaveChangesAsync() frees the thread while it waits. The async version returns a Task<int> that you await. Otherwise they do the same work. Both write the tracked changes in one transaction and return the number of entries written.
// Synchronous
int changesCount = context.SaveChanges();
// Asynchronous
changesCount = await context.SaveChangesAsync();
In a web app, a blocked thread cannot serve other requests, so async gives better scalability under high traffic. In a desktop app it keeps the UI responsive. Async does not make a single save faster.
Always await the call before you use the context again. EF Core does not support two operations running at the same time on one DbContext.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What are Data Annotations and how are they used in Entity Framework Core?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Data annotations are attributes you put on entity classes and properties to configure the model. EF Core reads them when it builds the model, so they shape the schema that migrations create.
public class Product
{
[Key]
public int ProductId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal Price { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }
[NotMapped]
public string DisplayName { get; set; }
}
[Key]marks the primary key[Required]makesNamea non-null column[MaxLength(100)]sets its length[Column(TypeName = ...)]picks the SQL type[ForeignKey]names the foreign key behind a navigation[NotMapped]leaves a property out of the table
Other common ones are [DatabaseGenerated], [Table] and [InverseProperty]. [Required], [MaxLength] and [Range] also drive validation in ASP.NET Core.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What are Migrations in Entity Framework Core and why are they important?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Migrations let you change the database schema step by step to match your entity model, without dropping the database and losing its data. Each migration is a C# class whose Up method applies a change and whose Down method reverts it.
They matter for four reasons.
- The schema history lives in source control next to the code.
- Teammates share schema changes by pulling and applying each other's migrations.
- The same migrations bring development, test and production databases to the same version.
- You can roll back by updating to an earlier migration.
# Add migration
dotnet ef migrations add InitialCreate
# Update database
dotnet ef database update
# Remove last migration
dotnet ef migrations remove
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
How do you create and apply migrations?
What are the different types of relationships in Entity Framework Core?
How do you configure One-to-Many relationships?
What are Navigation Properties and how do they work?
What is the difference between First(), FirstOrDefault(), Single(), and SingleOrDefault()?
Explain the different ways to configure DbContext.
What is Fluent API and when would you use it over Data Annotations?
How do you scaffold an existing database using Entity Framework Core?
How can you customize migration generation?
What happens when you have migration conflicts in a team environment?
How do you configure Many-to-Many relationships in EF Core 5+?
How do you handle self-referencing relationships?
What's the difference between IQueryable and IEnumerable in EF Core context?
How do you use Include() for eager loading and what are its limitations?
How do you write raw SQL queries in Entity Framework Core?
What is Change Tracking in Entity Framework Core and how does it work?
How do you disable Change Tracking for read-only scenarios?
What is the difference between Update() and modifying properties directly?
How do you detect property changes in Entity Framework Core?
How do you configure entity properties using Fluent API?
How do you configure indexes in Entity Framework Core?
How do you implement paging efficiently in Entity Framework Core?
How do you work with database transactions in Entity Framework Core?
How do you handle the "Cannot access a disposed object" error?
How do you implement Repository Pattern with Entity Framework Core?
How do you implement database seeding in Entity Framework Core?
What is the difference between eager, explicit and lazy loading in EF Core?
What are ExecuteUpdate and ExecuteDelete, and how do they differ from SaveChanges?
How does EF Core compare to Dapper, and when would you choose each?
What are Global Query Filters and how do you implement them?
What are Value Converters and how do you use them?
What are Shadow Properties and how do you use them?
What are some common performance issues in Entity Framework Core and how to avoid them?
What are compiled queries and when should you use them?
How do you use Split Queries to optimize performance?
How do you handle concurrency conflicts in Entity Framework Core?
What is the difference between pessimistic and optimistic locking?
What are Owned Types and when would you use them?
How do you implement Table Splitting in Entity Framework Core?
How do you implement Soft Delete using Global Query Filters?
What are Keyless Entity Types and when would you use them?
What are some Entity Framework Core best practices for production applications?
How do you handle complex business logic with Entity Framework Core?
What are some strategies for testing applications that use Entity Framework Core?
How do you implement multi-tenancy with Entity Framework Core?
How do you implement temporal tables (system-versioned tables) with EF Core?
How do you handle complex inheritance scenarios in Entity Framework Core?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
Entity Framework Core in pictures.
A quick recap of Entity Framework Core in 33 short animated pictures, one idea at a time. About 8 min to get the whole topic back in your head before you walk in. Pause any picture, slow it down, or step through it.
Pro The first chapter is free to play. The other 30 pictures come with Pro.
Entity Framework Core cheatsheet
- DbContext & entities01
- Map classes to tables02
- Relationships03
- Migrations & seed data04
- Query & load data05
- Track & save changes06
- Make queries faster07
- Soft delete & multi-tenancy08
- Advanced table mapping09
- Production, design & tests10
- The model11
- + 5 more inside
This picture is part of Pro. The first chapter is free to play; the other 30 pictures come with Pro.
Get Pro · $10/mo Already Pro? Sign in47 of 54 Entity Framework Core answers are in Pro.
Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.
- Full answers + code
- AI explanations, simpler or deeper
- 1,000 AI credits / month
- Cancel anytime
Change topic
Pick a different technology or stack. Your current topic stays put until you choose a new one.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsDjango
Python Full-Stack DevelopmentRuby on Rails
Convention over ConfigurationServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQInterviewers also test these - they're common to every stack, whichever one you picked above.
FastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDInterviewers also test these - they're common to every stack, whichever one you picked above.
AI Engineer
LLMs, RAG, Agents, EvalsAI-Powered Developer
Claude Code, Copilot, Agentic WorkflowsCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.