Interview Questions

Get ready for your next interview with our comprehensive question library

Entity Framework Core Interview Questions

Filter by Difficulty

1.

What are the main approaches to using Entity Framework Core?

beginner

EF Core supports three main approaches:

  1. Code First: Define entity classes first, then generate database schema
    • Start with C# classes (entities)
    • Use migrations to create and update database
    • Good for new projects with full control over data model
  2. Database First: Start with existing database, generate entity classes
    • Use Scaffold-DbContext command
    • Generates DbContext and entity classes from existing database
    • Good for existing databases or database-driven development
  3. Model First: Design model using visual designer (less common in EF Core)
    • Primarily supported through external tools
    • Not as prominent as in EF6
2.

What is an Entity in Entity Framework Core?

beginner

An Entity is a class that represents a table in the database. Each instance of an entity corresponds to a row in the table.

public class Product
{
    public int Id { get; set; }           // Primary key
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int CategoryId { get; set; }
    // Navigation property
    public Category Category { get; set; }
}

Requirements for an entity:

  • Must be a reference type (class)
  • Must have a parameterless constructor
  • Should have a primary key property
  • Properties must have public getters and setters (or protected setters)
3.

What is DbContext and what is its role in Entity Framework Core?

beginner

DbContext is the primary class responsible for interacting with the database. It represents a session with the database and acts as a Unit of Work pattern implementation.
Key responsibilities:

  • Database connection management: Manages database connections
  • Change tracking: Tracks changes to entities
  • Query execution: Translates LINQ queries to SQL
  • Transaction management: Handles database transactions
  • Entity lifecycle: Manages entity states (Added, Modified, Deleted, etc.)
public class ApplicationDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("connection-string");
    }
}
4.

What is DbSet and how is it used?

beginner

DbSet<T> represents a collection of entities of a specific type that can be queried from the database. It provides an abstraction over database tables.
Key features:

  • Querying: Supports LINQ queries
  • CRUD operations: Add, Remove, Update operations
  • Change tracking: Tracks entity states
  • Local view: Access to locally tracked entities
// Querying
var products = context.Products.Where(p => p.Price > 100).ToList();
// Adding
context.Products.Add(new Product { Name = "New Product" });
// Removing
context.Products.Remove(product);
// Local entities
var localProducts = context.Products.Local.ToList();
5.

What is the difference between DbContext.SaveChanges() and DbContext.SaveChangesAsync()?

beginner
  • SaveChanges(): Synchronous method that blocks the calling thread until database operations complete
  • SaveChangesAsync(): Asynchronous method that doesn't block the calling thread, returns Task<int>
// Synchronous
int changesCount = context.SaveChanges();
// Asynchronous
int changesCount = await context.SaveChangesAsync();

Benefits of async:

  • Better scalability in web applications
  • Doesn't block threads during I/O operations
  • Improves application responsiveness
  • Essential for high-traffic applications
6.

What are Data Annotations and how are they used in Entity Framework Core?

beginner

Data Annotations are attributes applied to entity classes and properties to configure the model and validation rules.
Common annotations:

public class Product
{
    [Key]
    public int ProductId { get; set; }
    [Required]
    [MaxLength(100)]
    public string Name { get; set; }
    [Column(TypeName = "decimal(18,2)")]
    public decimal Price { get; set; }
    [ForeignKey("CategoryId")]
    public Category Category { get; set; }
    [NotMapped]
    public string DisplayName { get; set; }
}

Categories:

  • Key attributes: [Key], [DatabaseGenerated]
  • Validation: [Required], [MaxLength], [Range]
  • Schema: [Table], [Column], [NotMapped]
  • Relationships: [ForeignKey], [InverseProperty]
7.

What are Migrations in Entity Framework Core and why are they important?

beginner

Migrations are a way to incrementally update the database schema to keep it in sync with the entity model while preserving existing data.
Benefits:

  • Version control: Track database schema changes
  • Team collaboration: Share schema changes across team
  • Deployment: Apply changes to different environments
  • Rollback: Undo changes if needed
  • Data preservation: Update schema without losing data
# Add migration
dotnet ef migrations add InitialCreate
# Update database
dotnet ef database update
# Remove last migration
dotnet ef migrations remove
8.

How do you create and apply migrations?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
9.

What are the different types of relationships in Entity Framework Core?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
10.

How do you configure One-to-Many relationships?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
11.

What are Navigation Properties and how do they work?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What is the difference between First(), FirstOrDefault(), Single(), and SingleOrDefault()?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
13.

Explain the different ways to configure DbContext.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

What is Fluent API and when would you use it over Data Annotations?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

How do you scaffold an existing database using Entity Framework Core?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

How can you customize migration generation?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

What happens when you have migration conflicts in a team environment?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

How do you configure Many-to-Many relationships in EF Core 5+?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

How do you handle self-referencing relationships?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

What's the difference between IQueryable and IEnumerable in EF Core context?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 52 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime