Entity Framework Core Interview

Migrations, Database Schema & Data Management

Entity Framework Core interview questions covering migrations, schema changes, indexes, keys, defaults, computed columns and data management.

25 Questions Filter by level

Interview questions

Open a question for the full answer, code and interview guidance.

25 Results
76
Mid-LevelIntermediate

What is the purpose of EF Core migrations?

EF Core migrations provide a versioned way to evolve a relational database schema as the application's EF Core model changes. A migration can describe operations such as creating or dropping tables, adding or altering columns, creating indexes, and changing constraints.

77
Mid-LevelIntermediate

How does EF Core determine what to put in a new migration?

When a new migration is created, EF Core compares the current model with the model snapshot associated with the previous migration. The differences are converted into migration operations.

78
Mid-LevelIntermediate

What are the Up and Down methods in an EF Core migration?

The Up method describes how to move the database forward to the migration's schema state. The Down method describes how to reverse that migration when a rollback is supported.

79
Mid-LevelIntermediate

What is the __EFMigrationsHistory table?

The __EFMigrationsHistory table records which EF Core migrations have been applied to a database. EF Core uses it to determine which migrations are still pending.

80
Mid-LevelIntermediate

How can you list migrations and determine whether migrations are pending?

EF Core tooling can list migrations known to the project, while application APIs can inspect applied and pending migrations for a database.

81
Mid-LevelIntermediate

What does Update-Database do in EF Core?

Update-Database applies migrations to the configured database until it reaches the requested migration, or the latest migration when no target is specified.

82
Mid-LevelIntermediate

How do you remove the last EF Core migration?

If the most recent migration has not been applied where it matters and no later migration depends on it, EF Core tooling can remove that migration and update the model snapshot.

83
Mid-LevelIntermediate

How can EF Core generate a SQL migration script?

EF Core tooling can generate SQL representing migration operations. A SQL script can be reviewed, archived, tested, and executed by a database administrator or deployment pipeline.

84
Mid-LevelIntermediate

What is an idempotent migration script?

An idempotent EF Core migration script contains logic that applies only migrations that have not already been recorded as applied. This allows the same script to be used against databases that may be at different known migration levels.

85
Mid-LevelIntermediate

Should an ASP.NET Core application automatically call Database.Migrate at startup?

Calling Database.Migrate at startup can be convenient for simple or tightly controlled deployments, but it is not universally appropriate.

86
Mid-LevelIntermediate

What is EnsureCreated and how is it different from migrations?

EnsureCreated creates a database schema directly from the current EF Core model when the database does not exist. It does not establish the normal migrations history used for incremental schema evolution.

87
Mid-LevelIntermediate

How do you configure table and column names in EF Core?

EF Core can map entity types and properties to specific table and column names using data annotations or the Fluent API.

88
Mid-LevelIntermediate

How do you configure column data types and lengths in EF Core?

Property configuration can specify maximum length, requiredness, precision, and provider-specific database column types.

89
Mid-LevelIntermediate

How do you configure default values in EF Core?

A property can be configured with a database default value or default SQL expression. The database uses the default when an INSERT does not provide a value for that column according to the mapping.

90
Mid-LevelIntermediate

What are computed columns in EF Core?

A computed column is calculated by the database from an expression rather than being supplied directly by ordinary application inserts and updates.

91
Mid-LevelIntermediate

How do you create indexes with EF Core?

Indexes can be configured in the EF Core model with HasIndex. A migration can then create the corresponding database index.

92
Mid-LevelIntermediate

How do you create a unique index in EF Core?

A unique index enforces that the indexed key value or key combination cannot be duplicated according to the database's uniqueness semantics.

93
Mid-LevelIntermediate

What are composite keys in EF Core?

A composite key uses more than one property to uniquely identify an entity. It is common in explicit join entities and some legacy database schemas.

94
Mid-LevelIntermediate

What are alternate keys in EF Core?

An alternate key is an additional candidate key that uniquely identifies an entity besides its primary key. EF Core relationships can target an alternate key.

95
Mid-LevelIntermediate

How do you map an entity to a database schema in EF Core?

For databases that support schemas, EF Core can map a table to a named schema. This can help organize database objects or integrate with an existing database design.

96
Mid-LevelIntermediate

What is data seeding with HasData in EF Core?

HasData configures model-managed seed data. EF Core migrations detect changes to this configured data and can generate insert, update, or delete operations.

97
Mid-LevelIntermediate

What are the risks of renaming a column or table through an EF Core migration?

A model rename may sometimes be interpreted as removing the old database object and creating a new one. If a generated migration drops a column and adds another, existing data can be lost.

98
Mid-LevelIntermediate

How should potentially destructive schema changes be deployed?

Destructive changes such as dropping columns, shrinking data types, or changing constraints should be planned around existing data and application compatibility.

99
Mid-LevelIntermediate

How can raw SQL be used for schema or data operations in an EF Core migration?

A migration can execute SQL directly through migrationBuilder.Sql when an operation cannot be conveniently expressed by standard migration APIs.

100
Mid-LevelIntermediate

What are best practices for EF Core migrations and schema management?

Keep migration files and the model snapshot in source control, give migrations meaningful names, and review generated operations before committing or deploying them. Never assume generated schema changes are automatically safe.