Entity Framework Core Interview Questions with Answers. (2024)

In previous article, I have mentionedDevOps interview questions and answersandASP.NET MVC Interview Questions and Answersbut in this article, I have mentioned Entity Framework Core (Ef Core) Interview questions with answers, which a beginner or advanced C#/.NET Core developer can read.

Entity Framework Core Interview Questions with Answers. (1)

  • What is Entity Framework Core?
  • What databases are supported to be used with EF core (Or which database does EF Core supports)?
  • How to install EF Core in .NET Core project?
  • What's New in EF Core 6.0
  • Mention all Entity states in EF Core
  • How do you add or update data in database using EF Core Entity state in same method?
  • How to add new table in database using EF Core Code First approach?
  • How do you resolve concurreny conflicts in Ef Core?
  • What other ORMs we can use with .NET based applications?
  • What do you mean by Dapper?

What is Entity Framework Core?

Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology.

EF Core can serve as an object-relational mapper (O/RM), which:

  1. Enables .NET developers to work with a database using .NET objects.
  2. Eliminates the need for most of the data-access code that typically needs to be written.

What databases are supported to be used with EF core (Or which database does EF Core supports)?

EF Core supports all the major databases like

  • SQL Server 2012 and above
  • SQLite
  • Azure Cosmos DB SQL API
  • PostgreSQL
  • MySQL
  • MariaDB
  • Oracle DB 11.2 onwards
  • Db2, Informix
  • Microsoft Access files
  • Teradata
  • Google Cloud Spanner
  • SQL Server Compact
  • Progress OpenEdge

How to install EF Core in .NET Core project?

You can use the .NET Core commmand line tools to install Entity Framework Core. Once you have created your project, navigate to the folder containing the .csproj (or project.json) file and execute the following command:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

You also need the Entity Framework Core tools if you want to make use of EF commands for migrations, scaffolding etc, so type the following command:

dotnet add package Microsoft.EntityFrameworkCore.Tools

OR

In Visual Studio, Go to Tools -> NuGet Package Manager -> Manage NuGet Packages For Solution and in Browse tab, search for "Entity framework Core", then install it.

What's New in EF Core 6.0

EF Core now supports:

  • The creation of temporal tables using Migrations
  • Transformation of existing tables into temporal tables, again using Migrations
  • Querying historical data
  • Restoring data from some point in the past

Mention all Entity states in EF Core

There are 5 entity states in EF Core:

  • Added: The entity is marked as added.
  • Deleted: The entity is marked as deleted.
  • Modified: The entity has been modified.
  • Unchanged: The entity hasn't been modified
  • Detached: The entity isn't tracked.

How do you add or update data in database using EF Core Entity state in same method?

So here is the example to add or update blog data in database, we will check if Blog data has Id, then we will udpate it otherwise we will give EntityState.Added.

public void InsertOrUpdate(Blog blog){ using (var context = new BloggingContext()) { context.Entry(blog).State = blog.BlogId == 0 ? EntityState.Added : EntityState.Modified; context.SaveChanges(); }}

How to add new table in database using EF Core Code First approach?

We will simply perform these steps:

  1. Add New Class in Models folders
  2. Next create a Database Context Class (DatabaseContext) and name it DbContext.cs. Place it inside the Models folder.
  3. Then Perform migration, by adding Migration first
    add-migration MigrationName?
  4. Then call update-database command using migration
    update-database?

How do you resolve concurreny conflicts in Ef Core?

There are three sets of values available to help resolve a concurrency conflict:

  • Current valuesare the values that the application was attempting to write to the database.
  • Original valuesare the values that were originally retrieved from the database, before any edits were made.
  • Database valuesare the values currently stored in the database.

The general approach to handle a concurrency conflicts is:

  1. CatchDbUpdateConcurrencyExceptionduringSaveChanges.
  2. UseDbUpdateConcurrencyException.Entriesto prepare a new set of changes for the affected entities.
  3. Refresh the original values of the concurrency token to reflect the current values in the database.
  4. Retry the process until no conflicts occur.

What other ORMs we can use with .NET based applications?

The following ORMs, we can use with .Net based applications are:

  • Dapper
  • NHibernate
  • Entity Framework 6.x

What do you mean by Dapper?

Dapper is a simple Object Mapper and is nothing but Object-relational mapping (ORM) and is responsible for mapping between database and programming language and also it owns the title of King of Micro ORM in terms of speed.

It is faster than Entity Framework.

You may also like to read:

OOPS interview questions in C# (With Answers)

Postman and API Testing Interview questions with answers

AWS EC2 Interview Questions and answer

Javascript interview questions and answers

Entity Framework Core Interview Questions with Answers. (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6067

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.