ASP.NET Core Entity Framework Core 1 — Questions and Answers
Question 1: What class is the primary entry point for Entity Framework Core database operations?
- DbContext (Correct answer)
- DatabaseContext
- EfContext
- DataContext
Correct answer: DbContext
DbContext manages entity objects during runtime, including querying, tracking, and saving changes to the database.
Question 2: Which method saves all pending changes in the EF Core DbContext to the database?
- SaveChangesAsync() (Correct answer)
- CommitAsync()
- FlushAsync()
- PersistAsync()
Correct answer: SaveChangesAsync()
SaveChangesAsync() generates and executes the SQL INSERT/UPDATE/DELETE statements for all tracked entity changes.
Question 3: What does DbSet<TEntity> represent in Entity Framework Core?
- A collection of all entities of a given type that can be queried and saved (Correct answer)
- A cached list of entities
- A database table definition file
- A set of raw SQL queries
Correct answer: A collection of all entities of a given type that can be queried and saved
DbSet<TEntity> maps to a database table and provides LINQ query methods for querying and manipulating that entity type.
Question 4: Which EF Core method retrieves an entity by its primary key from the DbContext?
- FindAsync() (Correct answer)
- FirstOrDefaultAsync()
- SingleAsync()
- GetByIdAsync()
Correct answer: FindAsync()
FindAsync() checks the local cache first before querying the database, returning null if not found.
Question 5: What is a migration in Entity Framework Core?
- A set of code files that record schema changes and can be applied to update the database (Correct answer)
- A SQL script run at deployment
- A backup of the current database state
- A version of the DbContext configuration
Correct answer: A set of code files that record schema changes and can be applied to update the database
Migrations capture the difference between the current model and the last migration, generating Up() and Down() methods for schema changes.
Question 6: Which CLI command creates a new EF Core migration?
- dotnet ef migrations add <MigrationName> (Correct answer)
- dotnet ef db update
- dotnet ef migration create
- dotnet ef schema add
Correct answer: dotnet ef migrations add <MigrationName>
dotnet ef migrations add generates a new migration file based on changes detected between the model and existing migrations.
What class is the primary entry point for Entity Framework Core database operations?