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.
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.
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.
Conceptual Models are the model classes which contain the relationships. These are independent of the database design.
Storage Models are our database design models, which contains database tables, views, stored procs and keys with relationships.
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:
pluralize and singularize in the Entity Framework?“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:
DBContext class?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.
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
}Lazy Loading, Eager Loading, and Explicit Loading?You can load related entities or data in EF in three ways
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.
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.
Advantages:
Disadvantages:
Advantages:
Disadvantages:
Below are the components of Entity Framework:
The ADO.NET Framework supports two models of Data Access Architecture:
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.
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);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.
CSDL, SSDL and MSL sections in an EDMX file?T4 templates?ObjectContext and DbContext?static instance)?.AsNoTracking() make?SaveChanges(false) + AcceptAllChanges()?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...