Explore the fundamentals of integrating Entity Framework Core with ASP.NET Core applications, focusing on configuration, data access, CRUD operations, and key best practices for robust backend development.
Which method is commonly used to register a DbContext for dependency injection in an ASP.NET Core application during startup?
Explanation: AddDbContext is the standard method to register a DbContext type with the dependency injection system in ASP.NET Core, allowing for lifecycle management and configuration. ConfigureDb and RegisterContext are not predefined methods in the framework and would not work out-of-the-box. SetDatabase sounds similar but does not exist for context registration.
Where is the recommended location to store a database connection string in an ASP.NET Core application using Entity Framework Core?
Explanation: appsettings.json is intended for storing configuration settings like connection strings, making it easy to manage and secure. Placing connection strings directly in the Controllers folder or Startup.cs class body reduces security and maintainability. Program log file is not relevant for configuration data.
Which method on DbContext should you call to persist changes, such as after adding a new entity in Entity Framework Core?
Explanation: SaveChanges applies all pending updates, deletions, or additions to the database, making it crucial for persisting data. CommitAll and WriteData are not actual Entity Framework Core methods, while UpdateDatabase relates more to migrations than standard data persistence.
Which Entity Framework Core feature lets you configure entity properties and relationships in code without using attributes?
Explanation: Fluent API provides a fluent interface for configuring the model in detail within the OnModelCreating method, avoiding attribute reliance. Scaffold API is for code generation, not configuration. Static Configuration and Inline Mapping are not established features in this context.
When retrieving data from a database, which query pattern is recommended to avoid loading all related entities by default?
Explanation: Lazy Loading delays retrieval of related data until it’s specifically accessed, improving efficiency. Immediate Join and Aggressive Retrieval are not established Entity Framework patterns, and Eager Writing is unrelated to reading data.
What is the main purpose of using migrations in Entity Framework Core for an ASP.NET Core project?
Explanation: Migrations provide a structured way to apply model changes to the database schema, ensuring consistency. Seeding data can be done via migrations but isn't their main purpose. Multi-language support and security are unrelated to database schema updates.
Which property is required on an entity type to allow proper table mapping in Entity Framework Core?
Explanation: A Primary Key uniquely identifies each entity, and its absence prevents proper table mapping in the database. Display Name and Description are optional metadata fields, and CreatedDate relates to timestamps, not identity.
What does a DbSet property in a DbContext class represent when building an ASP.NET Core API?
Explanation: A DbSet exposes operations for a particular entity type, handling queries and persistence. It is not an application service or a view model, and it is not related to static configuration data.
When performing updates, which feature of Entity Framework Core keeps track of the changes made to entities?
Explanation: Change Tracker monitors the state of entities and detects changes for update operations. History Logger, State Provider, and Update Watcher are not correct terms or features in this context.
Which query language is natively integrated with Entity Framework Core for querying data collections?
Explanation: LINQ is deeply integrated with Entity Framework Core, enabling expressive and type-safe queries. JQL is not related, SQLNet is not a known query language, and EntitySQL relates to older frameworks, not Entity Framework Core.