Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
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: Projection transforms entities into custom shapes, e.g., DTOs. var projected = context.Products Example code .Select(p => new { p.Name, p.Price }); Useful for: Reducing data load. Returning only needed f…
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…
Short answer: Stored procedures can be called via raw SQL or FromSqlRaw: var orders = context.Orders .FromSqlRaw("EXEC GetOrdersByCustomer @CustomerId = {0}", customerId) .ToList(); For non-query SPs, use Datab…
Short answer: Precompile frequently used queries to improve performance: private static readonly Func<MyDbContext, int, Product> _getProductById = EF.CompileQuery((MyDbContext ctx, int id) => ctx.Products.First(…
Short answer: Enable logging in DbContext options: optionsBuilder .UseSqlServer(connectionString) .LogTo(Console.WriteLine, LogLevel.Information); Or configure logging in ASP.NET Core logging pipeline. Repository Pattern…
Short answer: pplication. Pros: Decouples data access logic from business logic. Easier to mock/test. Encapsulates queries in a single place. Cons: EF Core’s DbContext already acts like a repository and unit of work. Can…
Short answer: Why use it with EF Core (pros and cons)? Repository Pattern abstracts data access logic, exposing CRUD methods to the application. Pros: Decouples data access logic from business logic. Easier to mock/test.…
Short answer: Unit of Work maintains a list of operations to be committed as a single transaction. EF Core’s DbContext implements Unit of Work, tracking changes and coordinating commits (SaveChanges()). Real-world exampl…
Short answer: Generic repository provides CRUD for all entity types. Advantages: Reusable and reduces boilerplate. Simple for basic CRUD. Disadvantages: Can become too generic, losing flexibility for complex queries. May…
Short answer: What are the trade-offs? Generic repository provides CRUD for all entity types. Advantages: Reusable and reduces boilerplate. Simple for basic CRUD. Disadvantages: Can become too generic, losing flexibility…
Short answer: public interface IRepository<T> where T : class { Task<T> GetByIdAsync(int id); Task<IEnumerable<T>> GetAllAsync(); Task AddAsync(T entity); void Update(T entity); void Delete(T enti…
Short answer: Sample signature / interface? public interface IRepository<T> where T : class { Task<T> GetByIdAsync(int id); Task<IEnumerable<T>> GetAllAsync(); Task AddAsync(T entity); void Update…
Short answer: public interface IUnitOfWork : IDisposable { IRepository<Customer> Customers { get; } IRepository<Order> Orders { get; } Task<int> CommitAsync(); } Implementation typically injects a singl…
Short answer: wait _unitOfWork.CommitAsync(); wait transaction.CommitAsync(); } catch { wait transaction.RollbackAsync(); throw; } wait _unitOfWork.CommitAsync(); wait transaction.CommitAsync(); } catch { wait transactio…
Short answer: Use DbContext transaction or IDbContextTransaction: using var transaction = await _context.Database.BeginTransactionAsync(); try { // multiple repository operations await _unitOfWork.CommitAsync(); Example…
Short answer: Mocking DbContext using frameworks like Moq (complex). Use InMemoryDatabase provider from EF Core for integration-like tests. Abstract dependencies and inject mocked interfaces for isolation. Performance, B…
Short answer: Properties not defined in your CLR classes but tracked by EF Core. Useful for storing metadata like CreatedBy, LastModified, or foreign keys without cluttering domain models. Real-world example (ShopNest) S…
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: Projection transforms entities into custom shapes, e.g., DTOs. var projected = context.Products
.Select(p => new { p.Name, p.Price }); Useful for: Reducing data load. Returning only needed fields. Mapping to custom types.
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.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Stored procedures can be called via raw SQL or FromSqlRaw: var orders = context.Orders .FromSqlRaw("EXEC GetOrdersByCustomer @CustomerId = {0}", customerId) .ToList(); For non-query SPs, use Database.ExecuteSqlRaw().
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: Precompile frequently used queries to improve performance: private static readonly Func<MyDbContext, int, Product> _getProductById = EF.CompileQuery((MyDbContext ctx, int id) => ctx.Products.First(p => p.Id == id)); // Usage: var product = _getProductById(context, 5);
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: Enable logging in DbContext options: optionsBuilder .UseSqlServer(connectionString) .LogTo(Console.WriteLine, LogLevel.Information); Or configure logging in ASP.NET Core logging pipeline. Repository Pattern & Unit of Work
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: pplication. Pros: Decouples data access logic from business logic. Easier to mock/test. Encapsulates queries in a single place. Cons: EF Core’s DbContext already acts like a repository and unit of work. Can add unnecessary abstraction and boilerplate. May limit EF Core’s powerful querying features.
ShopNest registers AppDbContext as scoped. One HTTP request = one context. Never inject it as a singleton.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Why use it with EF Core (pros and cons)? Repository Pattern abstracts data access logic, exposing CRUD methods to the application. Pros: Decouples data access logic from business logic. Easier to mock/test. Encapsulates queries in a single place. Cons: EF Core’s DbContext already acts like a repository and unit of work. Can add unnecessary abstraction and boilerplate. May limit EF Core’s powerful querying features.
ShopNest registers AppDbContext as scoped. One HTTP request = one context. Never inject it as a singleton.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Unit of Work maintains a list of operations to be committed as a single transaction. EF Core’s DbContext implements Unit of Work, tracking changes and coordinating commits (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: Generic repository provides CRUD for all entity types. Advantages: Reusable and reduces boilerplate. Simple for basic CRUD. Disadvantages: Can become too generic, losing flexibility for complex queries. May leak EF Core specifics or cause over-abstraction. Sometimes custom repositories per aggregate are better.
ShopNest registers AppDbContext as scoped. One HTTP request = one context. Never inject it as a singleton.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: What are the trade-offs? Generic repository provides CRUD for all entity types. Advantages: Reusable and reduces boilerplate. Simple for basic CRUD. Disadvantages: Can become too generic, losing flexibility for complex queries. May leak EF Core specifics or cause over-abstraction. Sometimes custom repositories per aggregate are better.
ShopNest registers AppDbContext as scoped. One HTTP request = one context. Never inject it as a singleton.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: public interface IRepository<T> where T : class { Task<T> GetByIdAsync(int id); Task<IEnumerable<T>> GetAllAsync(); Task AddAsync(T entity); void Update(T entity); void Delete(T entity); Task SaveChangesAsync(); } public interface IRepository<T> where T : class { Task<T> GetByIdAsync(int id); Task<IEnumerable<T>> GetAllAsync();… Task AddAsync(T entity);…… void Update(T entity); void Delete(T entity); Task…
SaveChangesAsync(); } public interface IRepository<T> where T : class { Task<T> GetByIdAsync(int id); Task<IEnumerable<T>> GetAllAsync(); Task AddAsync(T entity); void Update(T entity); void Delete(T entity); Task SaveChangesAsync(); } public interface IRepository<T> where T : class { Task<T> GetByIdAsync(int id); Task<IEnumerable<T>> GetAllAsync();… Task AddAsync(T entity); void Update(T entity); void Delete(T entity); Task SaveChangesAsync(); }
ShopNest registers AppDbContext as scoped. One HTTP request = one context. Never inject it as a singleton.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Sample signature / interface? public interface IRepository<T> where T : class { Task<T> GetByIdAsync(int id); Task<IEnumerable<T>> GetAllAsync(); Task AddAsync(T entity); void Update(T entity); void Delete(T entity); Task SaveChangesAsync(); }
ShopNest registers AppDbContext as scoped. One HTTP request = one context. Never inject it as a singleton.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: public interface IUnitOfWork : IDisposable { IRepository<Customer> Customers { get; } IRepository<Order> Orders { get; } Task<int> CommitAsync(); } Implementation typically injects a single DbContext instance shared across repositories. CommitAsync() calls DbContext.SaveChangesAsync().
public interface IUnitOfWork : IDisposable
{ IRepository<Customer> Customers { get; } IRepository<Order> Orders { get; } Task<int> CommitAsync(); } Implementation typically injects a single DbContext instance shared across repositories. CommitAsync() calls DbContext.SaveChangesAsync().
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: wait _unitOfWork.CommitAsync(); wait transaction.CommitAsync(); } catch { wait transaction.RollbackAsync(); throw; } wait _unitOfWork.CommitAsync(); wait transaction.CommitAsync(); } catch { wait transaction.RollbackAsync(); throw; } wait _unitOfWork.CommitAsync(); wait transaction.CommitAsync(); } catch { wait transaction.RollbackAsync(); throw; } wait… _unitOfWork.CommitAsync(); wait transaction.CommitAsync(); }…
catch { wait transaction.RollbackAsync(); throw; }
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 DbContext transaction or IDbContextTransaction: using var transaction = await _context.Database.BeginTransactionAsync(); try { // multiple repository operations await _unitOfWork.CommitAsync();
await transaction.CommitAsync();
} catch {
await transaction.RollbackAsync(); throw; }
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: Mocking DbContext using frameworks like Moq (complex). Use InMemoryDatabase provider from EF Core for integration-like tests. Abstract dependencies and inject mocked interfaces for isolation. Performance, Best Practices, & Advanced Features
ShopNest registers AppDbContext as scoped. One HTTP request = one context. Never inject it as a singleton.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Properties not defined in your CLR classes but tracked by EF Core. Useful for storing metadata like CreatedBy, LastModified, or foreign keys without cluttering domain models.
ShopNest’s order service uses EF Core to save an Order and its line items in one SaveChangesAsync() call inside a transaction.