Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: To remove the last migration before applying it: dotnet ef migrations remove To rollback the database to a previous migration: dotnet ef database update MigrationName To rollback all migrations: dotnet ef d…
Short answer: Generate SQL script using: dotnet ef migrations script To script a specific range: dotnet ef migrations script FromMigration ToMigration These scripts are useful for deploying to production environments or…
Short answer: How to generate SQL scripts for migrations? Generate SQL script using: dotnet ef migrations script To script a specific range: dotnet ef migrations script FromMigration ToMigration These scripts are useful…
Short answer: Use the HasData() method in OnModelCreating(): modelBuilder.Entity<User>().HasData( new User { Id = 1, Name = "Admin" } ); Data is inserted automatically during Update-Database. Changes to s…
Short answer: Best practices: Communicate and coordinate migration changes. Always pull and apply the latest migrations before adding a new one. If conflicts arise: Resolve merge conflicts in migration files manually. Re…
Short answer: Generate SQL script from tested migrations. Manually review and apply via DBA or DevOps tools. Use EF Core tools in CI/CD (e.g., GitHub Actions, Azure DevOps): dotnet ef database update --no-build ● Always…
Short answer: Scaffolded migrations are the automatically generated classes that contain Up() and Down() methods. They are generated based on model differences. You can manually edit these files to: Add custom SQL Rename…
Short answer: You can manually edit migration files after scaffolding: protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.Sql("UPDATE Users SET IsActive = 1 WHERE IsActive IS NULL"…
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: dded New entity to be inserted into the database Modified Entity has been changed and will be updated Deleted Entity will be deleted from the database Detached Entity is not tracked by the context Unchange…
Short answer: EF Core uses these EntityState values: State Description Added New entity to be inserted into the database Modified Entity has been changed and will be updated Deleted Entity will be deleted from the databa…
Short answer: re loaded, and compares them before saving. Notifications: If your entities implement INotifyPropertyChanged, EF can track changes immediately. EF compares the current values to the original snapshot during…
Short answer: EF Core tracks changes via: Snapshot change tracking: EF stores a snapshot of original values when entities are loaded, and compares them before saving. Notifications: If your entities implement INotifyProp…
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: You can disable tracking by default for a specific DbContext using: optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTrac king); Or per-query using .AsNoTracking(). Real-world example (ShopNe…
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: 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: 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: 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: 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…
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: To remove the last migration before applying it: dotnet ef migrations remove To rollback the database to a previous migration: dotnet ef database update MigrationName To rollback all migrations: dotnet ef database update 0
When ShopNest adds a DiscountCode column, you create a migration, review the SQL, then apply it on staging before production.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Generate SQL script using: dotnet ef migrations script To script a specific range: dotnet ef migrations script FromMigration ToMigration These scripts are useful for deploying to production environments or executing via CI/CD pipelines.
When ShopNest adds a DiscountCode column, you create a migration, review the SQL, then apply it on staging before production.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: How to generate SQL scripts for migrations? Generate SQL script using: dotnet ef migrations script To script a specific range: dotnet ef migrations script FromMigration ToMigration These scripts are useful for deploying to production environments or executing via CI/CD pipelines.
When ShopNest adds a DiscountCode column, you create a migration, review the SQL, then apply it on staging before production.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Use the HasData() method in OnModelCreating(): modelBuilder.Entity<User>().HasData( new User { Id = 1, Name = "Admin" } ); Data is inserted automatically during Update-Database. Changes to seed data require a new migration.
When ShopNest adds a DiscountCode column, you create a migration, review the SQL, then apply it on staging before production.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Best practices: Communicate and coordinate migration changes. Always pull and apply the latest migrations before adding a new one. If conflicts arise: Resolve merge conflicts in migration files manually. Rebuild ModelSnapshot as needed. Consider squashing or reorganizing migrations.
When ShopNest adds a DiscountCode column, you create a migration, review the SQL, then apply it on staging before production.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Generate SQL script from tested migrations. Manually review and apply via DBA or DevOps tools. Use EF Core tools in CI/CD (e.g., GitHub Actions, Azure DevOps): dotnet ef database update --no-build ● Always backup the database before applying migrations in production.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: Scaffolded migrations are the automatically generated classes that contain Up() and Down() methods. They are generated based on model differences. You can manually edit these files to: Add custom SQL Rename columns Seed data, etc.
When ShopNest adds a DiscountCode column, you create a migration, review the SQL, then apply it on staging before production.
Entity Framework Core Entity Framework Core Tutorial · EF Core
Short answer: You can manually edit migration files after scaffolding: protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.Sql("UPDATE Users SET IsActive = 1 WHERE IsActive IS NULL"); } Add indexes, constraints, stored procedures. Use raw SQL or Fluent API. Be cautious to maintain idempotency and correctness. Entity Framework Core – Change Tracking & Concurrency
When ShopNest adds a DiscountCode column, you create a migration, review the SQL, then apply it on staging before production.
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: dded New entity to be inserted into the database Modified Entity has been changed and will be updated Deleted Entity will be deleted from the database Detached Entity is not tracked by the context Unchange No changes have been made since it was loaded You can check/set the state via: context.Entry(entity).State = EntityState.Modified; Example… code dded… New… entity to be inserted into the database Modified Entity…
has been changed and will be updated Deleted Entity will be deleted from the database Detached Entity is not tracked by the context Unchange No changes have been made since it was loaded You can check/set the state via: context.Entry(entity).State = EntityState.Modified; dded New entity to be inserted into the database Modified Entity has been changed and will be updated Deleted Entity will be deleted from the database Detached Entity is not tracked by the context Unchange No changes have been made since it was loaded You can check/set the state via: context.Entry(entity).State = EntityState.Modified; Example… code dded New entity…
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 uses these EntityState values: State Description Added New entity to be inserted into the database Modified Entity has been changed and will be updated Deleted Entity will be deleted from the database Detached Entity is not tracked by the context Unchange No changes have been made since it was loaded You can check/set the state via: context.Entry(entity).State = EntityState.Modified;
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: re loaded, and compares them before saving. Notifications: If your entities implement INotifyPropertyChanged, EF can track changes immediately. EF compares the current values to the original snapshot during 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: EF Core tracks changes via: Snapshot change tracking: EF stores a snapshot of original values when entities are loaded, and compares them before saving. Notifications: If your entities implement INotifyPropertyChanged, EF can track changes immediately. EF compares the current values to the original snapshot during 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: 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: You can disable tracking by default for a specific DbContext using: optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTrac king); Or per-query using .AsNoTracking().
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: 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: 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: 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: 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: 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.