In previous article, I have mentioned DevOps interview questions and answers and ASP.NET MVC Interview Questions and Answers but 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.

ef-core-interview-questions-answers-min.jpg

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 values are the values that the application was attempting to write to the database.
  • Original values are the values that were originally retrieved from the database, before any edits were made.
  • Database values are the values currently stored in the database.

The general approach to handle a concurrency conflicts is:

  1. Catch DbUpdateConcurrencyException during SaveChanges.
  2. Use DbUpdateConcurrencyException.Entries to 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