Entity Framework Interview Questions and Answers
by Mohammed, on Mar 27, 2018 3:25:40 PM
Q1. What is Entity Framework in MVC?
Ans: The Microsoft ADO.NET Entity Framework is an Object/Relational Mapping(ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write.
Q2. What is the use of Dbcontext in Entity Framework?
Ans: The class that derives DbContext is called context class in entity framework.DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database. DbContext is the primary class that is responsible for interacting with the database.
Q3. Is Entity Framework an open source?
Ans: Entity Framework (EF) is an open source object-relational mapping (ORM)framework for ADO.NET. It was a part of .NET Framework, but since Entity framework version 6 it is separated from .NET framework.
Q4. What is a EDMX file in Entity Framework?
Ans: An .edmx file is an XML file that defines a conceptual model , a storage model , and the mapping between these models. An .edmx file also contains information that is used by the ADO.NET Entity Data Model Designer (Entity Designer) to render a model graphically.
Q5. What are the components of Entity Framework Architecture?
Ans: 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 Provide.
Q6. What are CSDL, SSDL and MSL sections in an EDMX file?
Ans: It is part of EDMX file which help to interact with data base;
- CSDL (Conceptual Schema Definition Language): it contains the conceptual abstraction of your database tables which will interact with user, it create the end point for user to interact with.
- SSDL (Storage Schema Definition Language): It is abstraction for data structure it is the first door by which database is interacting to pass schema from database.
- MSL (Mapping Schema Language) : it use to make the bridge between the CSDL and SSDL.
Q7. What is LINQ?
Ans: Language-Integrated Query (LINQ) is a way to query data without cumbersome stored procedures. Previously, programmers needed to create stored procedures and then call these stored procedures from their code. With Entity framework, you can pull data and query it using language similar to SQL.
Q8. Does Entity framework support primary and foreign keys?
Ans: Yes, Entity framework supports both types of primary and foreign keys. You can define these in your database tables and import them to your model classes. If you don’t already have a database set up, you can create these keys in your data model classes and their respective data modeling classes.
Q9. What is Entity Graph in entity framework?
Ans: Entity graph is nothing but to show the relationship between the entities. In EDMX there we have multiple tables and graph shows the relation of entity as one to one, one to many or many to many relation.
Q10. What is Code first approach?
Ans: In code first approach we have the code as bunch of POCO classes but we don’t have Database. Through the help of code only it will generate the database. You have full control over on your code in code first approach.
Q11. What are the types of property in Entity Framework?
Ans: We have three types of property in entity framework:
- Scalar property
- Navigation Property
- Complex Property
Q12. What is lazy loading?
Ans: Lazy loading is a way to return only objects that are used. When you query the database model, lazy loading only returns the immediate tables needed by the user. All related tables are returned when they are used. This means you can reserve memory and storage when you work with large programs. It also means that objects are created until you need them, so it makes your program faster.
Q13. How to disable the lazy loading framework?
Ans: You can disable the lazy loading by
context.ContextOptions.LazyLoadingEnabled = false;
Q14. Difference between LINQ to SQL and Entity Framework?
Ans:
Entity Framework | LINQ to SQL |
It create the EDMX file to work | It create DBML file to work. |
It work for any type of data base, SQL Server, DB2, My SQL etc. | It works for only SQL server database. |
It enable to query database objects using DBContext and ObjectContext | It enables to query using data using DataContext. |
Entity Framework you can map class with multiple tables. | Classes must be one-to-one with database object with tables. |
Entity frame work support Model first, DB first and Code first approach. | It support only DB first approach. |
Q15. What is eager loading?
Ans: Eager loading is the opposite of lazy loading. When you query for objects, eager loading returns all of the objects including the related objects. For instance, when you query a list of customers and orders, eager loading loads all objects including the customers and the orders instead of just what you originally need (customers).
Q16. What is a navigation property?
Ans: A navigation property points to related tables in your database model. For instance, if you have a customer table that relates to the orders table, the navigation property points from the customers table to the orders table. While the primary and foreign keys are physical properties of the table, the navigation properties are a logical part of a data model. When you view the Entity framework model, you can view the navigation properties to better understand the structure of your tables.
Q17. What is POCO class in Entity Framework?
Ans: POCO stands for plain old CLR object classes. If you generate the entity framework classes POCO classes generate automatically by system with multiple attributes. It’s generated based on the table you have in the database. It will be most probably like below. It is a simple class.
Q18. How to handle concurrency in Entity Framework?
Ans: There are two types of locking; Optimistic Locking and Pessimistic Locking.
- Optimistic Locking: It assumes that multiple transactions will work without affecting each other. In Other word no locks are enforced while doing Optimistic locking, the transaction just verify that no other transition has modified the data. In case of modification transaction will roll back.
- Pessimistic Locking: Pessimistic locking assumes that concurrency/collision issue will happen so a lock is placed on the records and then data is updated. We can do the pessimistic locking by specifying the Isolation level in SQL server store procedure.
In entity frame work we can do only optimistic locking. That we can set from EDMX Designer.
Just open the table and select any of the Row, than only set the property.
Q19. What are scalar and navigation properties in Entity Framework?
Ans:
- Scalar properties are the property which is directly links with the database.
e.g. Department name, Department Id are Scalar property. - Navigation Properties are property which is association with the other entity property.
e.g. Employees is Navigation property.
Q20. What is client wins and store wins mode in Entity Framework concurrency?
Ans: Client and Store wins are actions which we need to take when concurrency happens. In store Wins, data are loaded into entity objects and in Client wins data will store from client side to database.