1 - What is Entity Framework core?


EF Core is a modern object-database mapper (ORM) for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core works with SQL Server, Azure SQL Database, SQLite, Azure Cosmos DB, MySQL, PostgreSQL, and other databases through a provider plugin API.

2 - What is a Model?


A model is a class that represents data. In terms of Entity Framework, a model represents the data from a table in the database. Usually, a model is just a simple POCO (Plain Old CRL Object).

3 - Explain DbContext Class in Entity Framework Core?


DbContext class is the brain of Entity Framework Core, using it you can do all the communications with the database. It allows performing the following tasks:

  • Managing database connection.
  • Configuring entities and the relationships between them.
  • Reading, creating, updating & deleting data in the database.
  • Configuring change tracking.
  • Caching
  • Transaction management

4 - What is the use of AsNoTracking()?


Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context. This ensures minimal memory usage and optimal performance.

5 - Explain how you can load related entities in EF?


Entity Framework Core allows you to use the navigation properties in your model to load related entities. 

There are three common ORM patterns used to load related data:

  • Eager loading means that the related data is loaded from the database as part of the initial query.

  • Explicit loading means that the related data is explicitly loaded from the database at a later time.

  • Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed.

6 - What is the different between add and attach in Entity Framework Core?


Add as the verb suggests is for adding new objects to the EF context in order for them to be created/inserted in the database when calling SaveChanges. 

Attach is used for when you didn’t retrieve this object from the database but I am going to let you know that this object now exists as I programmatically instantiated it in custom code.

7 - When queries are executed?


When you call LINQ operators, you are simply building up an in-memory representation of the query. The query is only sent to the database when the results are consumed.

The most common operations that result in the query being sent to the database are:

  • Iterating the results in a for loop.

  • Using an operator such as ToList, ToArray, Single, Count

8 - How to handle concurrency in EF?


You can use the IsConcurrencyToken method to configure a property as a concurrency token.

Properties configured as concurrency tokens are used to implement optimistic concurrency control: whenever an update or delete operation is performed during SaveChanges, the value of the concurrency token on the database is compared against the original value read by EF Core.

  • If the values match, the operation can complete.

  • If the values do not match, EF Core assumes that another user has performed a conflicting operation and aborts the current transaction.

9 - How to improve EF performance?


There are some considerations when using EF that can improve performance considerably:

  • Consider using the NoTracking option for reading operations.
  • Improve response time by using pre-generating views.
  • Fetch only the required fields from the database.
  • Avoid getting big amounts of data by using pagination.
  • Use indexes correctly.
  • Debug and Optimize LINQ query.

10 - How to use transactions in EF?


By default, all changes in a single call to SaveChanges() are applied in a transaction. If any of the changes fail, then the transaction is rolled back and none of the changes are applied to the database. This means that SaveChanges() is guaranteed to either completely succeed, or leave the database unmodified if an error occurs. 

It is also possible to control transactions by calling the method context.
BeginTransaction() or using the object TransactionScope.