Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
.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.User…
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 Example: var user…
Answer: You can disable tracking by default for a specific DbContext using: optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTrac king); Or per-query using .AsNoTracking(). What interviewers expect A clear…
Use the Entry() method: context.Entry(entity).State = EntityState.Modified; context.Entry(entity).State = EntityState.Detached; This is helpful when: You’re updating data without reloading it Working with disconnected sc…
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-b…
What is optimistic concurrency vs pessimistic concurrency? Optimistic concurrency: Assumes that conflicts are rare. EF checks for changes before saving using concurrency tokens. Pessimistic concurrency: Locks records in…
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 h…
EF Core supports the standard types of relationships: One-to-One (1:1) – One entity has one related entity. One-to-Many (1:N) – One entity relates to many others. Many-to-Many (M:N) – Many entities relate to many others.…
Example using Fluent API: modelBuilder.Entity<User>() .HasOne(u => u.Profile) .WithOne(p => p.User) .HasForeignKey<Profile>(p => p.UserId); Or with data annotations: public class Profile { [Key, Fore…
Fluent API: modelBuilder.Entity<Blog>() .HasMany(b => b.Posts) .WithOne(p => p.Blog) .HasForeignKey(p => p.BlogId); Data annotations: public class Post { public int BlogId { get; set; } [ForeignKey("BlogId…
In EF Core 5.0+, you can define many-to-many relationships without an explicit join entity: public class Student { public ICollection<Course> Courses { get; set; } } public class Course { public ICollection<Stud…
join table maps a many-to-many relationship. In EF Core: ✅ Auto-created (EF Core 5+): If you define many-to-many with ICollection<T>, EF creates the join table. 🛠 Manual (custom join entity): public class StudentC…
How to represent them in EF Core? A join table maps a many-to-many relationship. In EF Core: ✅ Auto-created (EF Core 5+): If you define many-to-many with ICollection<T>, EF creates the join table. 🛠 Manual (custom…
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")] publ…
How to define them via annotations & 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; } […
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&lt;OrderDetail&gt;() .HasKey(od =&…
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(Delete…
shadow property is a property not defined in the .NET class but exists in the EF model. Example: modelBuilder.Entity<Post>() .HasOne<Blog>() .WithMany() .HasForeignKey("BlogId"); // BlogId is a shadow FK if n…
In Fluent API: Required: .HasRequired(p => p.Blog) Optional: .HasOne(p => p.Blog) .WithMany() .IsRequired(false); EF infers: Reference type (e.g., Blog) → optional by default Non-nullable value type FK (e.g., int B…
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 .ThenIncl…
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(…
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…
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.EntityF…
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.Fi…
When & how to use it? 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: v…
Entity Framework Core Entity Framework Core Tutorial · EF Core
.AsNoTracking() tells EF Core not to track entities in the returned query.
✅ Use when:
Example:
var users = context.Users.AsNoTracking().ToList();Entity Framework Core Entity Framework Core Tutorial · EF Core
When to use it?
.AsNoTracking() tells EF Core not to track entities in the returned query.
✅ Use when:
Example:
var users = context.Users.AsNoTracking().ToList();
Entity Framework Core Entity Framework Core Tutorial · EF Core
Answer: You can disable tracking by default for a specific DbContext using: optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTrac king); Or per-query using .AsNoTracking().
In a production Entity Framework Core application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Use the Entry() method:
context.Entry(entity).State = EntityState.Modified;
context.Entry(entity).State = EntityState.Detached;
This is helpful when:
Entity Framework Core Entity Framework Core Tutorial · EF Core
before saving using concurrency tokens.
supported out-of-the-box in EF Core).
EF Core supports optimistic concurrency using [ConcurrencyCheck] or
[Timestamp].
Entity Framework Core Entity Framework Core Tutorial · EF Core
What is optimistic concurrency vs
pessimistic concurrency?
before saving using concurrency tokens.
supported out-of-the-box in EF Core).
EF Core supports optimistic concurrency using [ConcurrencyCheck] or
[Timestamp].
Entity Framework Core Entity Framework Core Tutorial · EF Core
concurrency token is a property used to detect concurrency conflicts.
Example with [Timestamp]:
[Timestamp]
public byte[] RowVersion { get; set; }
DbUpdateConcurrencyException.
Entity Framework Core – Relationships
Entity Framework Core Entity Framework Core Tutorial · EF Core
EF Core supports the standard types of relationships:
Each of these can be configured in EF Core using navigation properties and Fluent API or
Data Annotations.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Example using Fluent API:
modelBuilder.Entity<User>()
.HasOne(u => u.Profile)
.WithOne(p => p.User)
.HasForeignKey<Profile>(p => p.UserId);
Or with data annotations:
public class Profile
{
[Key, ForeignKey("User")]
public int UserId { get; set; }
public User User { get; set; }
}
Entity Framework Core Entity Framework Core Tutorial · EF Core
Fluent API:
modelBuilder.Entity<Blog>()
.HasMany(b => b.Posts)
.WithOne(p => p.Blog)
.HasForeignKey(p => p.BlogId);
Data annotations:
public class Post
{
public int BlogId { get; set; }
[ForeignKey("BlogId")]
public Blog Blog { get; set; }
}
Entity Framework Core Entity Framework Core Tutorial · EF Core
In EF Core 5.0+, you can define many-to-many relationships without an explicit join entity:
public class Student
{
public ICollection<Course> Courses { get; set; }
}
public class Course
{
public ICollection<Student> Students { get; set; }
}
EF will automatically create a join table CourseStudent behind the scenes.
If you want to customize the join table, define a join entity explicitly.Entity Framework Core Entity Framework Core Tutorial · EF Core
join table maps a many-to-many relationship. In EF Core:
✅ Auto-created (EF Core 5+):
If you define many-to-many with ICollection<T>, EF creates the join table.
🛠 Manual (custom join entity):
public class StudentCourse
{
public int StudentId { get; set; }
public Student Student { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
}
nd configure with Fluent API.
Entity Framework Core Entity Framework Core Tutorial · EF Core
How to represent them in EF
Core?
A join table maps a many-to-many relationship. In EF Core:
✅ Auto-created (EF Core 5+):
If you define many-to-many with ICollection<T>, EF creates the join table.
🛠 Manual (custom join entity):
public class StudentCourse
public int StudentId { get; set; }
public Student Student { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
And configure with Fluent API.
Entity Framework Core Entity Framework Core Tutorial · EF Core
nnotations & Fluent API?
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);Entity Framework Core Entity Framework Core Tutorial · EF Core
How to define them via
annotations & Fluent API?
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);
Entity Framework Core Entity Framework Core Tutorial · EF Core
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 });
In a production Entity Framework Core application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Entity Framework Core Entity Framework Core Tutorial · EF Core
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:
EF Core defaults to Cascade for required relationships.
Entity Framework Core Entity Framework Core Tutorial · EF Core
shadow property is a property not defined in the .NET class but exists in the EF model.
Example:
modelBuilder.Entity<Post>()
.HasOne<Blog>()
.WithMany()
.HasForeignKey("BlogId"); // BlogId is a shadow FK if not
defined in class
EF tracks it internally but you can't access it in C# code directly.
Entity Framework Core Entity Framework Core Tutorial · EF Core
In Fluent API:
.HasRequired(p => p.Blog)
.HasOne(p => p.Blog)
.WithMany()
.IsRequired(false);
EF infers:
Entity Framework Core – Lazy vs
Eager vs Explicit Loading
Entity Framework Core Entity Framework Core Tutorial · EF Core
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.
Entity Framework Core Entity Framework Core Tutorial · EF Core
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 problem.
Entity Framework Core Entity Framework Core Tutorial · EF Core
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
options.UseLazyLoadingProxies();
public virtual ICollection<Post> Posts { get; set; }
ccessed.
Entity Framework Core Entity Framework Core Tutorial · EF Core
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.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Explicit loading means loading related data manually, after the main entity is loaded.
Use when:
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
Entity Framework Core Entity Framework Core Tutorial · EF Core
When & how to use it?
Explicit loading means loading related data manually, after the main entity is loaded.
Use when:
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