Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: // Create context.Users.Add(new User { Name = "John" }); context.SaveChanges(); // Read var user = context.Users.FirstOrDefault(u => u.Id == 1); // Update user.Name = "Jane"; context.…
Short answer: Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# code (POCOs) Easily version-con…
Short answer: Advantages and when to use it Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# c…
Short answer: Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy databases ❌ Cons: Harder to vers…
Short answer: Use‑cases and pros and cons Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy data…
Short answer: Model First allows creating a database model using a designer (EDMX file), then generating both the database and code from that model. ⚠ EF Core does not support Model First. This approach was available in…
Short answer: Is it used in EF Core? Model First allows creating a database model using a designer (EDMX file), then generating both the database and code from that model. ⚠ EF Core does not support Model First. This app…
Short answer: A migration snapshot is a C# file (e.g., ModelSnapshot.cs) automatically created by EF Core. It represents the current model state. EF uses it to compare changes in future migrations. Located in the Migrati…
Short answer: fter they are loaded from the database. This allows EF Core to generate the correct SQL INSERT, UPDATE, or DELETE statements when SaveChanges() is called. Real-world example (ShopNest) Read-only catalog pag…
Short answer: Change tracking is the process by which EF Core keeps track of changes made to entities after they are loaded from the database. This allows EF Core to generate the correct SQL INSERT, UPDATE, or DELETE sta…
Short answer: re monitored and persisted. Untracked queries: EF does not monitor the returned entities. re monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only ccess. T…
Short answer: Tracked queries: EF Core tracks entities returned from the query. Changes to them are monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only access. Tracked…
Short answer: .AsNoTracking() tells EF Core not to track entities in the returned query. Explain a bit more ✅ Use when: Read-only operations Improving performance for large queries Preventing memory overhead of tracking…
Short answer: When to use it? .AsNoTracking() tells EF Core not to track entities in the returned query. ✅ Use when: Read-only operations Improving performance for large queries Preventing memory overhead of tracking Exa…
Short answer: Optimistic concurrency: Assumes that conflicts are rare. EF checks for changes before saving using concurrency tokens. Pessimistic concurrency: Locks records in the DB to prevent other updates (not supporte…
Short answer: A concurrency token is a property used to detect concurrency conflicts. Example with [Timestamp]: [Timestamp] public byte[] RowVersion { get; set; } EF Core includes this column in the WHERE clause of updat…
Short answer: nnotations & Fluent API? Primary Key (PK): Unique identifier of a record. Foreign Key (FK): A field that references a PK in another table. Data Annotations: [Key] public int Id { get; set; } [ForeignKey…
Short answer: composite key is a primary key made of multiple columns. EF Core does not support composite keys via data annotations, so you must use Fluent PI: modelBuilder.Entity<OrderDetail>() .HasKey(od => ne…
Short answer: How to define composite keys in EF Core? A composite key is a primary key made of multiple columns. EF Core does not support composite keys via data annotations, so you must use Fluent API: modelBuilder.Ent…
Short answer: Cascade delete ensures that related entities are deleted when the parent entity is deleted. Configure with Fluent API: modelBuilder.Entity<Blog>() .HasMany(b => b.Posts) .WithOne(p => p.Blog) .O…
Short answer: Eager loading loads related data as part of the initial query, reducing round-trips to the database. ✅ Use .Include() to load related entities: var blogs = context.Blogs .Include(b => b.Posts) .ToList();…
Short answer: How is .Include() / .ThenInclude() used? Eager loading loads related data as part of the initial query, reducing round-trips to the database. ✅ Use .Include() to load related entities: var blogs = context.B…
Short answer: re proxies? Lazy loading delays the loading of related data until it's accessed for the first time. EF Core requires proxies for lazy loading: Install NuGet: Microsoft.EntityFrameworkCore.Proxies Enable in…
Short answer: How to enable lazy loading in EF Core? What are proxies? Lazy loading delays the loading of related data until it's accessed for the first time. EF Core requires proxies for lazy loading: Install NuGet: Mic…
Short answer: Explicit loading means loading related data manually, after the main entity is loaded. Explain a bit more Use when: You need full control over what and when to load You don’t want automatic lazy loading var…
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: // Create context.Users.Add(new User { Name = "John" }); context.SaveChanges(); // Read var user = context.Users.FirstOrDefault(u => u.Id == 1); // Update user.Name = "Jane"; context.SaveChanges(); // Delete context.Users.Remove(user); context.SaveChanges();
// Create context.Users.Add(new User { Name = "John" }); context.SaveChanges(); // Read var user = context.Users.FirstOrDefault(u => u.Id == 1); // Update user.Name = "Jane"; context.SaveChanges(); // Delete context.Users.Remove(user); context.SaveChanges();
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# code (POCOs) Easily version-controlled Supports migrations to handle schema evolution Works well with Domain-Driven Design 📌 When to use: When you're starting a new project or prefer designing schema in code.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Advantages and when to use it Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# code (POCOs) Easily version-controlled Supports migrations to handle schema evolution Works well with Domain-Driven Design 📌 When to use: When you're starting a new project or prefer designing schema in code.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy databases ❌ Cons: Harder to version control Schema changes must be manually re-scaffolded 📌 When to use: For integrating with existing databases or legacy systems.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Use‑cases and pros and cons Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy databases ❌ Cons: Harder to version control Schema changes must be manually re-scaffolded 📌 When to use: For integrating with existing databases or legacy systems.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Model First allows creating a database model using a designer (EDMX file), then generating both the database and code from that model. ⚠ EF Core does not support Model First. This approach was available in EF6 with Visual Studio tooling.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Is it used in EF Core? Model First allows creating a database model using a designer (EDMX file), then generating both the database and code from that model. ⚠ EF Core does not support Model First. This approach was available in EF6 with Visual Studio tooling.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: A migration snapshot is a C# file (e.g., ModelSnapshot.cs) automatically created by EF Core. It represents the current model state. EF uses it to compare changes in future migrations. Located in the Migrations folder, named like [YourDbContext]ModelSnapshot.cs.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: fter they are loaded from the database. This allows EF Core to generate the correct SQL INSERT, UPDATE, or DELETE statements when SaveChanges() is called.
Read-only catalog pages use AsNoTracking() so EF Core does not spend time snapshotting entities you will never update.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Change tracking is the process by which EF Core keeps track of changes made to entities after they are loaded from the database. This allows EF Core to generate the correct SQL INSERT, UPDATE, or DELETE statements when SaveChanges() is called.
Read-only catalog pages use AsNoTracking() so EF Core does not spend time snapshotting entities you will never update.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: re monitored and persisted. Untracked queries: EF does not monitor the returned entities. re monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only ccess. Tracked (default): var user = context.Users.FirstOrDefault(); Untracked: var user = context.Users.AsNoTracking().FirstOrDefault(); re monitored and…… persisted. Untracked queries: EF does not monitor the returned…
entities. Faster, read-only ccess. Tracked (default): var user = context.Users.FirstOrDefault(); Untracked: var user = context.Users.AsNoTracking().FirstOrDefault(); re monitored and persisted. Untracked queries: EF does not monitor the returned entities. re monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only ccess. Tracked (default): var user = context.Users.FirstOrDefault(); Untracked: var user = context.Users.AsNoTracking().FirstOrDefault(); re monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only ccess. Tracked (default):…
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Tracked queries: EF Core tracks entities returned from the query. Changes to them are monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only access. Tracked (default): var user = context.Users.FirstOrDefault(); Untracked: var user = context.Users.AsNoTracking().FirstOrDefault();
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: .AsNoTracking() tells EF Core not to track entities in the returned query.
✅ Use when: Read-only operations Improving performance for large queries Preventing memory overhead of tracking var users = context.Users.AsNoTracking().ToList(); .AsNoTracking() tells EF Core not to track entities in the returned query. .AsNoTracking() tells EF Core not to track entities in the returned query. ✅ Use when: Read-only operations Improving performance for large queries Preventing memory overhead of tracking
var users = context.Users.AsNoTracking().ToList(); .AsNoTracking() tells EF Core not to track entities in the returned query. .AsNoTracking() tells EF Core not to track entities in the returned query. ✅ Use when: Read-only operations Improving performance for large queries Preventing memory overhead of tracking Example: var users = context.Users.AsNoTracking().ToList(); .AsNoTracking() tells EF Core not to track entities in the returned query. ✅ Use when: Read-only operations Improving performance for large queries Preventing memory overhead of tracking Example: var users = context.Users.AsNoTracking().ToList();
Read-only catalog pages use AsNoTracking() so EF Core does not spend time snapshotting entities you will never update.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: When to use it? .AsNoTracking() tells EF Core not to track entities in the returned query. ✅ Use when: Read-only operations Improving performance for large queries Preventing memory overhead of tracking
var users = context.Users.AsNoTracking().ToList();
Read-only catalog pages use AsNoTracking() so EF Core does not spend time snapshotting entities you will never update.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Optimistic concurrency: Assumes that conflicts are rare. EF checks for changes before saving using concurrency tokens. Pessimistic concurrency: Locks records in the DB to prevent other updates (not supported out-of-the-box in EF Core). EF Core supports optimistic concurrency using [ConcurrencyCheck] or [Timestamp].
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: A concurrency token is a property used to detect concurrency conflicts. Example with [Timestamp]: [Timestamp] public byte[] RowVersion { get; set; } EF Core includes this column in the WHERE clause of updates. If the row has changed since it was loaded, EF will throw a DbUpdateConcurrencyException. Entity Framework Core – Relationships
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: nnotations & Fluent API? Primary Key (PK): Unique identifier of a record. Foreign Key (FK): A field that references a PK in another table. Data Annotations: [Key] public int Id { get; set; } [ForeignKey("Blog")] public int BlogId { get; set; } Fluent API: modelBuilder.Entity<Post>() .HasKey(p => p.Id); modelBuilder.Entity<Post>() .HasOne(p =>… p.Blog)……… WithMany(b => b.Posts) .HasForeignKey(p => p.BlogId);…
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: composite key is a primary key made of multiple columns. EF Core does not support composite keys via data annotations, so you must use Fluent PI: modelBuilder.Entity<OrderDetail>() .HasKey(od => new { od.OrderId, od.ProductId }); composite key is a primary key made of multiple columns.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: How to define composite keys in EF Core? A composite key is a primary key made of multiple columns. EF Core does not support composite keys via data annotations, so you must use Fluent API: modelBuilder.Entity<OrderDetail>() .HasKey(od => new { od.OrderId, od.ProductId });
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Cascade delete ensures that related entities are deleted when the parent entity is deleted. Configure with Fluent API: modelBuilder.Entity<Blog>() .HasMany(b => b.Posts) .WithOne(p => p.Blog) .OnDelete(DeleteBehavior.Cascade); Delete behaviors: Cascade Restrict SetNull NoAction EF Core defaults to Cascade for required relationships.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Eager loading loads related data as part of the initial query, reducing round-trips to the database. ✅ Use .Include() to load related entities: var blogs = context.Blogs .Include(b => b.Posts) .ToList(); Use .ThenInclude() for deeper nesting: context.Blogs .Include(b => b.Posts) .ThenInclude(p => p.Comments); Eager loading prevents lazy load performance issues and the N+1 problem.
Loading orders with items uses .Include(o => o.Items). Without Include, listing 50 orders can trigger 50 extra queries (N+1).
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: How is .Include() / .ThenInclude() used? Eager loading loads related data as part of the initial query, reducing round-trips to the database. ✅ Use .Include() to load related entities: var blogs = context.Blogs .Include(b => b.Posts) .ToList(); Use .ThenInclude() for deeper nesting: context.Blogs .Include(b => b.Posts) .ThenInclude(p => p.Comments); Eager loading prevents lazy load performance issues and the N+1…
Loading orders with items uses .Include(o => o.Items). Without Include, listing 50 orders can trigger 50 extra queries (N+1).
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: re proxies? Lazy loading delays the loading of related data until it's accessed for the first time. EF Core requires proxies for lazy loading: Install NuGet: Microsoft.EntityFrameworkCore.Proxies Enable in OnConfiguring or AddDbContext: options.UseLazyLoadingProxies(); Mark navigation properties as virtual: public virtual ICollection<Post>… Posts { get;…… set;… } EF creates runtime proxies to override navigation…
Loading orders with items uses .Include(o => o.Items). Without Include, listing 50 orders can trigger 50 extra queries (N+1).
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: How to enable lazy loading in EF Core? What are proxies? Lazy loading delays the loading of related data until it's accessed for the first time. EF Core requires proxies for lazy loading: Install NuGet: Microsoft.EntityFrameworkCore.Proxies Enable in OnConfiguring or AddDbContext: options.UseLazyLoadingProxies(); Mark navigation properties as virtual: public virtual ICollection<Post> Posts { get; set; } EF creates…
runtime proxies to override navigation properties and load them when accessed.
Loading orders with items uses .Include(o => o.Items). Without Include, listing 50 orders can trigger 50 extra queries (N+1).
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Explicit loading means loading related data manually, after the main entity is loaded.
Use when: You need full control over what and when to load You don’t want automatic lazy loading var blog = context.Blogs.First(); context.Entry(blog) .Collection(b => b.Posts) .Load(); context.Entry(blog) .Reference(b => b.Owner) .Load(); ✅ Use .Reference().Load() for single navigation ✅ Use .Collection().Load() for collections Explicit loading means loading related data manually, after the main entity is loaded. Explicit loading means loading related data manually, after the main entity is loaded. Use when: You need full control over what and when to load You don’t want automatic lazy loading
var blog = context.Blogs.First(); context.Entry(blog) .Collection(b => b.Posts) .Load(); context.Entry(blog) .Reference(b => b.Owner) .Load(); ✅ Use .Reference().Load() for single navigation ✅ Use .Collection().Load() for collections Explicit loading means loading related data manually, after the main entity is loaded. Explicit loading means loading related data manually, after the main entity is loaded. Use when: You need full control over what and when to load You don’t want automatic lazy loading Example: var blog = context.Blogs.First(); context.Entry(blog) .Collection(b => b.Posts) .Load(); context.Entry(blog) .Reference(b => b.Owner) .Load(); ✅ Use .Reference().Load() for single navigation ✅ Use .Collection().Load() for collections Explicit loading means loading related data manually, after the main entity is loaded. Use when: You need full control over what and when to load You don’t want automatic lazy loading Example: var blog = context.Blogs.First(); context.Entry(blog) .Collection(b => b.Posts) .Load(); context.Entry(blog) .Reference(b => b.Owner) .Load(); ✅ Use .Reference().Load() for single navigation ✅ Use .Collection().Load() for collections
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.