Entity Framework Core with ASP.NET Core Fundamentals Quiz Quiz

Challenge your understanding of Entity Framework Core integration with ASP.NET Core. Explore key concepts such as DbContext, migrations, LINQ queries, and data relationships to reinforce essential data access skills in web applications.

  1. DbContext Purpose

    What is the primary role of the DbContext class when working with Entity Framework Core in an ASP.NET Core application?

    1. It defines the web server’s configuration settings.
    2. It controls URL routing for the web application.
    3. It manages database connections and tracks changes to entities.
    4. It handles only authentication and security functions.

    Explanation: DbContext acts as the main bridge between your code and the database, managing entity tracking and database transactions. The second option about configuration settings is related to the hosting environment, not data access. The third option, dealing with authentication, is handled by identity or middleware, not DbContext. The last option refers to URL routing, which is unrelated to Entity Framework’s responsibilities.

  2. Adding Entity Framework Core

    Which package must you install to enable Entity Framework Core in a typical ASP.NET Core project?

    1. Microsoft.Data.Tools
    2. Microsoft.AspNetCore.Mvc
    3. System.Data.SqlClient
    4. Microsoft.EntityFrameworkCore

    Explanation: Microsoft.EntityFrameworkCore is the main package required to use Entity Framework Core within your application. Microsoft.AspNetCore.Mvc is for MVC pattern support, not data access. System.Data.SqlClient is a legacy data provider and not specific to Entity Framework Core. Microsoft.Data.Tools relates to database tooling, not the ORM functionality.

  3. Registering DbContext

    How do you register a DbContext class for dependency injection in an ASP.NET Core application's startup process?

    1. Instantiate it in the Main method.
    2. Declare it with a public static modifier in Program.cs.
    3. Use services.AddDbContextu003CYourContextu003E() in the ConfigureServices method.
    4. Add it to the appsettings.json file directly.

    Explanation: Registering with services.AddDbContext in the ConfigureServices method ensures the context is available via dependency injection. Adding it to appsettings.json only stores configuration data and does not register services. Instantiation in the Main method or using public static does not integrate with dependency injection properly.

  4. Creating a Migration

    Which command is used to scaffold a new migration representing changes in your Entity Framework Core model?

    1. dotnet build new migration
    2. dotnet ef migrations add
    3. add-migration .net
    4. dotnet ef update database

    Explanation: The 'dotnet ef migrations add' command generates a new migration based on model changes. 'dotnet build new migration' is not a valid command. 'dotnet ef update database' applies migrations, not creates them. 'add-migration .net' is a common typo and incorrect.

  5. LINQ Query Syntax

    Which LINQ syntax retrieves all items from a DbSet called Products where the price is greater than 50?

    1. Products.FindAll(p =u003E p.Price u003E 50)
    2. Products.Filter(p =u003E p.Price u003E 50)
    3. Products.Where(p =u003E p.Price u003E 50)
    4. Products.SelectAll(p =u003E p.Price u003E 50)

    Explanation: Where is the correct LINQ extension method to filter entities based on a condition. SelectAll is not a valid method in this context. Filter and FindAll are commonly confused with Where but are either not available or belong to other APIs.

  6. Database Provider Specification

    In the OnConfiguring or AddDbContext method, which method is typically called to specify the database provider for Entity Framework Core?

    1. SetDatabase
    2. AssignDataSource
    3. UseSqlServer
    4. ConfigureProvider

    Explanation: UseSqlServer is the standard method for specifying SQL Server as the database provider in Entity Framework Core. The other methods, SetDatabase, ConfigureProvider, and AssignDataSource, are incorrect or do not exist in this context.

  7. Eager Loading

    When querying data with Entity Framework Core, which method is used to include related navigation properties in the query results?

    1. Include
    2. LoadWith
    3. Attach
    4. Fetch

    Explanation: Include is used in LINQ queries to eagerly load related data. Attach is used to associate an existing entity with the context for tracking. Fetch and LoadWith are not available in Entity Framework Core and are misleading distractors.

  8. Model Validation

    Which attribute would you apply to a property in a model to ensure a value must be provided for that property?

    1. Required
    2. Key
    3. StringLength
    4. ForeignKey

    Explanation: The Required attribute enforces that a property is not null and must have a value. Key is used to designate primary keys. StringLength sets length constraints but does not enforce presence. ForeignKey is for defining relationships, not value requirement.

  9. Primary Key Convention

    By default, how will Entity Framework Core treat a property named 'Id' in a class called 'Order'?

    1. It will ignore the 'Id' property entirely.
    2. It will configure 'Id' as the primary key for the 'Order' entity.
    3. It will require an explicit [Key] attribute on 'Id' to recognize it.
    4. It will map 'Id' as a foreign key by default.

    Explanation: A property named 'Id' in an entity class is automatically treated as the primary key by convention. The property will not be ignored; it does not default to being a foreign key, and it does not require the [Key] attribute unless you want customization.

  10. Saving Changes

    After making changes to tracked entities, which method commits those changes to the database in Entity Framework Core?

    1. PushChanges
    2. ApplyNow
    3. CommitData
    4. SaveChanges

    Explanation: SaveChanges is the correct method to persist changes made to tracked entities. ApplyNow, CommitData, and PushChanges do not exist and are incorrect choices that may be confused with similar concepts.