FullStackFSCCafé
 
 
Sign in with GoogleSign in with Google. Opens in new tab
Kill Your Tech Interview
3877 Full-Stack, Algorithms & System Design Interview Questions
Answered To Get Your Next Six-Figure Job Offer
      
🤖 Having Machine Learning & DS Interview? Check  MLStack.Cafe - 1704 Data Science & ML Interview Questions & Answers!Having ML & DS Interview? Check 🤖 MLStack.Cafe - 1704 ML & DS Interview Questions and Answers

35 Entity Framework Interview Questions (ANSWERED + .NET Core Updates)

Microsoft introduced Entity Framework in 2008 with . NET Framework 3.5. Since then, it released many versions of Entity Framework. Currently, there are two latest versions of Entity Framework: EF 6 and EF Core. Follow along to refresh your knowledge around 35 most common Entity Framework Interview Questions and Answers every .NET developer may face on their tech interview.

Q1: 
What is Entity Framework?

Answer

ADO.NET EF is an ORM (object-relational mapping) which creates a higher abstract object model over ADO.NET components. So rather than getting into dataset, datatables, command, and connection objects as shown in the below code, you work on higher level domain objects like customers, suppliers, etc.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q2: 
What is Code First approach in Entity Framework?

Answer

In Code First approach we avoid working with the Visual Designer of Entity Framework. In other words the EDMX file is excluded from the solution. So you now have complete control over the context class as well as the entity classes.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q3: 
What is Conceptual Model?

Answer

Conceptual Models are the model classes which contain the relationships. These are independent of the database design.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q4: 
What is Storage Model?

Answer

Storage Models are our database design models, which contains database tables, views, stored procs and keys with relationships.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q5: 
What is migration in Entity Framework?

Answer

Entity Framework introduced a migration tool that automatically updates the database schema when your model changes without losing any existing data or other database objects.

There are two kinds of Migration:

  • Automated Migration
  • Code-based Migration

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q6: 
What is pluralize and singularize in the Entity Framework?

Answer

“Pluralize” and “Singularize” give meaningful naming conventions to objects. In simple words it says do you want to represent your objects with the below naming convention:

  • One Customer record means “Customer” (singular).
  • Lot of customer records means “Customer’s” (plural, watch the “s”)

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q7: 
What is the purpose of a DBContext class?

Answer

You can think of DbContext as the database connection and a set of tables, and DbSet as a representation of the tables themselves. The DbContext allows you to link your model properties (presumably using the Entity Framework) to your database with a connection string.

Later, when you wish to refer to a database in your controller to handle data, you reference the DbContext.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q8: 
Can you explain Lazy Loading in a detailed manner?

Answer

Lazy loading is a concept where we load objects on demand rather than loading everything in one go. Consider a situation where you have 1 to many relationships between the Customer and Address objects. Now let’s say you are browsing the customer data but you do not want address data to be loaded at that moment. But the time you start accessing the address object you would like to load address data from the database.

Entity Framework has lazy loading behavior by default enabled. For instance, consider the below code. When we are doing a foreach on the Customer object, the Address object is not loaded. But the time you start doing foreach on the address collection, the Address object is loaded from SQL Server by firing SQL queries.

So in simple words, it will fire a separate query for each address record of the customer, which is definitely not good for a large number of records.

MyEntities context = new MyEntities();

var Customers = context.Customers.ToList();

foreach (Customercust in Customers) // In this line no address object loaded
{
     foreach(Address add in cust.Addresses){}// Address object is loaded here
}

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions
🤖 Having Machine Learning & DS Interview? Check  MLStack.Cafe - 1704 Data Science & ML Interview Questions & Answers!Having ML & DS Interview? Check 🤖 MLStack.Cafe - 1704 ML & DS Interview Questions and Answers

Q9: 
Explain Lazy Loading, Eager Loading, and Explicit Loading?

Answer
  • Lazy Loading: It is a process to delay the loading of related objects until it is required.
  • Eager Loading: It occurs when you query for an object and all of the related objects are also returned. In eager loading, related objects are loaded automatically with its parent object
  • Explicit Loading: Explicitly loading takes place when you have disabled Lazy loading, and you still want to lazy loading. For this, we have to call the load method on the related entities.

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q10: 
Explain how you can load related entities in EF?

Answer

You can load related entities or data in EF in three ways

  • Eager Loading
  • Lazy Loading
  • Explicit Loading

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q11: 
How can we handle concurrency in Entity Framework?

Answer

In EF, concurrency issue is resolved by using optimistic locking. To implement optimistic locking, right click on the EDMX designer and set the concurrency mode to Fixed. Now whenever we have concurrency issues you should get an OptimisticConcurrencyException error.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q12: 
What are POCO classes in Entity Framework?

Answer

POCO means Plain Old C# Object. When EDMX creates classes, they are cluttered with a lot of entity tags. Many times we would like to use simple .NET classes and integrate them with Entity Framework.

Entity Framework allows this. In other words you can create a simple .NET class and use the entity context object to load your simple .NET classes.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q13: 
What are the advantages/disadvantages of Code First Approach?

Answer

Advantages:

  • Based on business objects we can decide the database structure.
  • We can decide which classes need to be serialized and can specify the collection to eager load.
  • Good for smaller applications.

Disadvantages:

  • All database related stuffs should be included in the code.
  • For Stored Procs, we need to use the Fluent APIs to write it in code.
  • Not good for data intensive applications.

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q14: 
What are the advantages of Model First Approach?

Answer
  • Model first approach gives the flexibility to design the Entity Models independently and gives an option to improve at later stages.
  • Model classes can be created by drawing it in the edmx designer, so no much of database is required.

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q15: 
What are the advantages and disadvantages of Database First Approach?

Answer

Advantages:

  • Easy to create entity models if there is an existing database.
  • Preferred approach for data intensive applications.

Disadvantages:

  • Once we create a edmx file from an existing database, huge pile of code is generated.
  • If we want to add the additional functionality to the models generated, we need to extend the models.

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions
🤖 Having Machine Learning & DS Interview? Check  MLStack.Cafe - 1704 Data Science & ML Interview Questions & Answers!Having ML & DS Interview? Check 🤖 MLStack.Cafe - 1704 ML & DS Interview Questions and Answers

Q16: 
What are the components of Entity Framework Architecture?

Answer

Below are the components of Entity Framework:

  • Entity Data Model (EDM)
  • LINQ to Entities
  • Entity SQL
  • Object Service
  • Entity Client Data Provider
  • ADO.Net Data Provider

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q17: 
What is EF Data Access Architecture?

Answer

The ADO.NET Framework supports two models of Data Access Architecture:

  • Connection Oriented
  • Disconnected

EF uses the Disconnected model (doesn't leave a connection open). Since you work with data and make desired changes and then you perform the SaveChanges. In N-tier applications a context either produces entities or saves entities, not both. It produces entities that get serialized and the context is disposed. Entities that return from the client application are deserialized and re-attached to new context instance that saves their changes.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q18: 
What is Eager Loading?

Answer

The opposite of lazy loading is eager loading. In eager loading we load the objects beforehand. So the first thing is we need to disable lazy loading by setting LazyLoadingEnabled to false.

context.ContextOptions.LazyLoadingEnabled = false;

Now we have to explicitly tell EF what objects we want to load by using the include function. Below is a simple sample code where we tell EF to load customer as well as address objects by using the include function.

Now the customer object and the related address objects will be loaded in one query rather than multiple queries.

var employees = context.Customers.Include("Addresses").Take(5);

Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q19: 
What is Optimistic Locking?

Answer

Optimistic Locking is a strategy where you read a record, take note of a version number (other methods to do this involve dates, timestamps or checksums/hashes) and check that the version hasn't changed before you write the record back. When you write the record back you filter the update on the version to make sure it's atomic. (i.e. hasn't been updated between when you check the version and write the record to the disk) and update the version in one hit.

If the record is dirty (i.e. different version to yours) you abort the transaction and the user can re-start it.

This strategy is most applicable to high-volume systems and three-tier architectures where you do not necessarily maintain a connection to the database for your session. In this situation the client cannot actually maintain database locks as the connections are taken from a pool and you may not be using the same connection from one access to the next.


Having Tech or Coding Interview? Check 👉 57 Entity Framework Interview Questions

Q20: 
Can you explain CSDL, SSDL and MSL sections in an EDMX file?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q21: 
Could you explain Pessimistic locking?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q22: 
What are T4 templates?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe
🤖 Having Machine Learning & DS Interview? Check  MLStack.Cafe - 1704 Data Science & ML Interview Questions & Answers!Having ML & DS Interview? Check 🤖 MLStack.Cafe - 1704 ML & DS Interview Questions and Answers

Q23: 
What is faster - ADO.NET or ADO.NET Entity Framework?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q24: 
What is the difference between POCO, Code First, and simple EF approach?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q25: 
What is the difference between ObjectContext and DbContext?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q26: 
What’s the difference between LINQ to SQL and Entity Framework?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q27: 
When would you use EF6 vs EF Core?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q28: 
Which type of loading is good in which scenario?

Answer
Join FullStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 120k+ Developer Who Trust FullStack.Cafe

Q29: 
Can I use Entity Framework 6 in .Net Core?

Answer
Unlock FullStack.Cafe to open all answers and get your next figure job offer!
Share this blog post to open Expert question!
🤖 Having Machine Learning & DS Interview? Check  MLStack.Cafe - 1704 Data Science & ML Interview Questions & Answers!Having ML & DS Interview? Check 🤖 MLStack.Cafe - 1704 ML & DS Interview Questions and Answers

Q30: 
How can we do pessimistic locking in Entity Framework?

Answer
Unlock FullStack.Cafe to open all answers and get your next figure job offer!
Share this blog post to open Expert question!

Q31: 
How do I view the SQL generated by the Entity Framework?

Answer
Unlock FullStack.Cafe to open all answers and get your next figure job offer!
Share this blog post to open Expert question!

Q32: 
What are the advantages and disadvantages of creating a Global Entities Context for the application (i.e. one static instance)?

Answer
Unlock FullStack.Cafe to open all answers and get your next figure job offer!
Share this blog post to open Expert question!

Q33: 
What difference does .AsNoTracking() make?

Answer
Unlock FullStack.Cafe to open all answers and get your next figure job offer!
Share this blog post to open Expert question!

Q34: 
What is the difference between Automatic Migration vs Code-base Migration?

Answer
Unlock FullStack.Cafe to open all answers and get your next figure job offer!
Share this blog post to open Expert question!

Q35: 
When would you use SaveChanges(false) + AcceptAllChanges()?

Answer
Unlock FullStack.Cafe to open all answers and get your next figure job offer!
Share this blog post to open Expert question!
 

Rust has been Stack Overflow’s most loved language for four years in a row and emerged as a compelling language choice for both backend and system developers, offering a unique combination of memory safety, performance, concurrency without Data races...

Clean Architecture provides a clear and modular structure for building software systems, separating business rules from implementation details. It promotes maintainability by allowing for easier updates and changes to specific components without affe...

Azure Service Bus is a crucial component for Azure cloud developers as it provides reliable and scalable messaging capabilities. It enables decoupled communication between different components of a distributed system, promoting flexibility and resili...

Cosmos DB has gained popularity among developers and organizations across various industries, including finance, e-commerce, gaming, IoT, and more. Follow along and learn the 24 most common and advanced Azure Cosmos DB interview questions and answers...
More than any other NoSQL database, and dramatically more than any relational database, MongoDB's document-oriented data model makes it exceptionally easy to add or change fields, among other things. It unlocks Iteration on the project. Iteration f...
Unit Tests and Test Driven Development (TDD) help you really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outpu...
Domain-Driven Design is nothing magical but it is crucial to understand the importance of Ubiquitous Language, Domain Modeling, Context Mapping, extracting the Bounded Contexts correctly, designing efficient Aggregates and etc. before your next DDD p...
At its core, Microsoft Azure is a public cloud computing platform - with solutions including Infrastructure as a Service (IaaS), Platform as a Service (PaaS), and Software as a Service (SaaS) that can be used for services such as analytics, virtual c...
As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. Follow along to refresh your knowledge and explore the 52 most frequently asked and advanced Node JS Interview Questions and Answers every...
Dependency Injection is most useful when you're aiming for code reuse, versatility and robustness to changes in your problem domain. DI is also useful for decoupling your system. DI also allows easier unit testing without having to hit a database and...