Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: context.Entry(entity).State = EntityState.Detached; This is helpful when: You’re updating data without reloading it Working with disconnected scenarios (e.g., APIs) Example code Use the Entry() method: cont…
Short answer: 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: Loc…
Short answer: 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 t…
Short answer: 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 Profil…
Short answer: 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; } [Fore…
Short answer: In EF Core 5.0+, you can define many-to-many relationships without an explicit join entity: public class Student Example code { public ICollection<Course> Courses { get; set; } } public class Course {…
Short answer: 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. { Example code public int StudentId { g…
Short answer: 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. 🛠…
Short answer: How to define them via annotations & Fluent API? Explain a bit more Primary Key (PK): Unique identifier of a record. Foreign Key (FK): A field that references a PK in another table. Data Annotations: [K…
Short answer: A shadow property is a property not defined in the .NET class but exists in the EF model. Example code modelBuilder.Entity<Post>() .HasOne<Blog>() .WithMany() .HasForeignKey("BlogId");…
Short answer: 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 F…
Short answer: Loading Type Pros Cons Eager Fewer queries, good for large data sets Loads everything even if not used Lazy Loads only when needed Risk of N+1 queries, more round-trips Explicit Fine-grained control More co…
Short answer: Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at all. You can also disable it for specific navigation properties by not making them vir…
Short answer: How? Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at all. You can also disable it for specific navigation properties by not making the…
Short answer: EF Core translates LINQ queries into SQL using expression trees. Only supported LINQ operators and expressions can be translated. Limitations: Certain C# methods or complex expressions cannot be translated…
Short answer: What limitations are there? EF Core translates LINQ queries into SQL using expression trees. Only supported LINQ operators and expressions can be translated. Limitations: Certain C# methods or complex expre…
Short answer: IQueryable<T>: Represents a query against the data source. Query is translated to SQL and executed on the database. Supports deferred execution. IEnumerable<T>: Represents an in-memory collectio…
Short answer: Inner join Example code var query = from c in context.Customers join o in context.Orders on c.Id equals o.CustomerId select new { c.Name, o.OrderDate }; Left join (using DefaultIfEmpty()): var query = from…
Short answer: var groupedData = context.Orders .GroupBy(o => o.CustomerId) .Select(g => new { CustomerId = g.Key, TotalOrders = g.Count(), TotalAmount = g.Sum(o => o.Amount), MaxAmount = g.Max(o => o.Amount),…
Short answer: Filtering: var filtered = context.Products.Where(p => p.Price > 100); Ordering: var ordered = context.Products .OrderBy(p => p.Category) .ThenByDescending(p => p.Price); Real-world example (Shop…
Short answer: First(): Returns the first element, throws if none found. FirstOrDefault(): Returns first element or default (null) if none. Single(): Returns the single element, throws if zero or more than one. SingleOrDe…
Short answer: Use .Skip() and .Take() for pagination: var page2 = context.Products .OrderBy(p => p.Id) .Skip(10) // Skip first 10 .Take(10) // Take next 10 .ToList(); Use .Skip() and .Take() for pagination: var page2…
Short answer: (Skip, Take) Use .Skip() and .Take() for pagination: var page2 = context.Products .OrderBy(p => p.Id) .Skip(10) // Skip first 10 .Take(10) // Take next 10 .ToList(); Real-world example (ShopNest) ShopNes…
Short answer: Use FromSqlRaw() or FromSqlInterpolated(): var products = context.Products .FromSqlRaw("SELECT * FROM Products WHERE Price > {0}", 100) .ToList(); Use FromSqlRaw() or FromSqlInterpolated(): var…
Short answer: (FromSqlRaw, etc.) Use FromSqlRaw() or FromSqlInterpolated(): var products = context.Products .FromSqlRaw("SELECT * FROM Products WHERE Price > {0}", 100) .ToList(); Real-world example (ShopNes…
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: context.Entry(entity).State = EntityState.Detached; This is helpful when: You’re updating data without reloading it Working with disconnected scenarios (e.g., APIs)
Use the Entry() method: context.Entry(entity).State = EntityState.Modified;
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: 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 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: 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. Each of these can be configured in EF Core using navigation properties and Fluent API or Data Annotations.
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: 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; }
} Requires a unique foreign key. EF assumes optional by default unless marked as required.
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: 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; } } A foreign key exists in the many-side entity (Post). EF infers this relationship from navigation + FK.
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; }
} A foreign key exists in the many-side entity (Post). EF infers this relationship from navigation + FK.
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: 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.
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: 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. {
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.
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 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.
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 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; } [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: A shadow property is a property not defined in the .NET class but exists in the EF model.
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.
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: 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 BlogId) → required Entity Framework Core – Lazy vs Eager vs Explicit Loading
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Loading Type Pros Cons Eager Fewer queries, good for large data sets Loads everything even if not used Lazy Loads only when needed Risk of N+1 queries, more round-trips Explicit Fine-grained control More code complexity ✅ Eager is best for performance when you know you'll need related data. ❌ Lazy can hurt performance unless used carefully (e.g., in UI apps).
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at all. You can also disable it for specific navigation properties by not making them virtual. Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at… all. You can also disable…… it for specific navigation properties by not making them…
virtual. Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at all. You can also disable it for specific navigation properties by not making them virtual. Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at… all. You can also disable it for specific navigation properties by not making them virtual.
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? Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at all. You can also disable it for specific navigation properties by not making them virtual.
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: EF Core translates LINQ queries into SQL using expression trees. Only supported LINQ operators and expressions can be translated. Limitations: Certain C# methods or complex expressions cannot be translated and cause runtime exceptions. Client-side evaluation may occur, which can hurt performance. Use .AsEnumerable() to switch to client-side processing intentionally.
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: What limitations are there? EF Core translates LINQ queries into SQL using expression trees. Only supported LINQ operators and expressions can be translated. Limitations: Certain C# methods or complex expressions cannot be translated and cause runtime exceptions. Client-side evaluation may occur, which can hurt performance. Use .AsEnumerable() to switch to client-side processing intentionally.
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: IQueryable<T>: Represents a query against the data source. Query is translated to SQL and executed on the database. Supports deferred execution. IEnumerable<T>: Represents an in-memory collection. LINQ operators execute in memory. Usually results after the query has executed and data is loaded.
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: Inner join
var query = from c in context.Customers join o in context.Orders on c.Id equals o.CustomerId select new { c.Name, o.OrderDate }; Left join (using DefaultIfEmpty()): var query = from c in context.Customers join o in context.Orders on c.Id equals o.CustomerId into orders from o in orders.DefaultIfEmpty() select new { c.Name, OrderDate = o != null ? o.OrderDate : (DateTime?)null };
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: var groupedData = context.Orders .GroupBy(o => o.CustomerId) .Select(g => new { CustomerId = g.Key, TotalOrders = g.Count(), TotalAmount = g.Sum(o => o.Amount), MaxAmount = g.Max(o => o.Amount), MinAmount = g.Min(o => o.Amount) });
var groupedData = context.Orders .GroupBy(o => o.CustomerId) .Select(g => new { CustomerId = g.Key, TotalOrders = g.Count(), TotalAmount = g.Sum(o => o.Amount), MaxAmount = g.Max(o => o.Amount), MinAmount = g.Min(o => o.Amount) });
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: Filtering: var filtered = context.Products.Where(p => p.Price > 100); Ordering: var ordered = context.Products .OrderBy(p => p.Category) .ThenByDescending(p => p.Price);
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: First(): Returns the first element, throws if none found. FirstOrDefault(): Returns first element or default (null) if none. Single(): Returns the single element, throws if zero or more than one. SingleOrDefault(): Returns the single element or default if none; throws if more than one.
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 .Skip() and .Take() for pagination: var page2 = context.Products .OrderBy(p => p.Id) .Skip(10) // Skip first 10 .Take(10) // Take next 10 .ToList(); Use .Skip() and .Take() for pagination: var page2 = context.Products .OrderBy(p => p.Id) .Skip(10) // Skip first 10 .Take(10) // Take next 10 .ToList(); Real-world example… (ShopNest) ShopNest’s order…… service uses EF Core to save an Order and its line items in one…
SaveChangesAsync() call inside a transaction. Use .Skip() and .Take() for pagination: var page2 = context.Products .OrderBy(p => p.Id) .Skip(10) // Skip first 10 .Take(10) // Take next 10 .ToList(); Use .Skip() and .Take() for pagination: var page2 = context.Products .OrderBy(p => p.Id) .Skip(10) // Skip first 10 .Take(10) // Take next 10 .ToList(); Real-world example… (ShopNest) 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: (Skip, Take) Use .Skip() and .Take() for pagination: var page2 = context.Products .OrderBy(p => p.Id) .Skip(10) // Skip first 10 .Take(10) // Take next 10 .ToList();
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 FromSqlRaw() or FromSqlInterpolated(): var products = context.Products .FromSqlRaw("SELECT * FROM Products WHERE Price > {0}", 100) .ToList(); Use FromSqlRaw() or FromSqlInterpolated(): var products = context.Products .FromSqlRaw("SELECT * FROM Products WHERE Price > {0}", 100) .ToList(); Use FromSqlRaw() or FromSqlInterpolated(): var products = context.
Products .FromSqlRaw("SELECT * FROM Products WHERE Price > {0}", 100) .ToList(); Use FromSqlRaw() or FromSqlInterpolated(): var products = context.Products .FromSqlRaw("SELECT * FROM Products WHERE Price > {0}", 100) .ToList();
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: (FromSqlRaw, etc.) Use FromSqlRaw() or FromSqlInterpolated(): var products = context.Products .FromSqlRaw("SELECT * FROM Products WHERE Price > {0}", 100) .ToList();
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.