1. What is Entity Framework?
Entity Framework is based on Object Relation model (ORM) framework developed by Microsoft.
It will automate the Why of interaction between database and your code. It makes your application as rapid develop by overriding for creating of framework for all your ADO.net.
It make easier to access Data base using linq query.
You can access the tables and storeprocedure detail in c# code and perform the crud operation very easily.
2. What its advantage?
Advantage of using entity framework is to generate the automated code to interact with data base. Its make your application in fast pace of development.
3. What is EDMX file in Entity Framework?
EDMX(Entity Data Model XML) file is a XML file that defines the CSDL, MSL and SSDL definitions. It means it contains the definition of storage model, Conceptual model and mapping between this two.
4. What are CSDL, SSDL and MSL sections in an EDMX file?
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.
If you see the EDMX you will find there is separate Nodes for each Section as below, which is auto generated by system only.
5. What is defining query in Entity framework.
A defining query will allow you to execute a SQL statement that is specified in the DefiningQuery element of an .edmx file.
6. What is association set in Entity Framework?
Association shows the logical relation between the tables in EDMX file.
7. How to add and update the EDMX file?
Use below link to know how to add entity file:
8. What is PropertyRef in entity framework?
PropertyRef element is in Storage schema definition language (SSDL) of EDMX file. It generally used to define the entity key. It used to define the identity columns for your table column.
9. Difference between LINQ to SQL and 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.
10. What is T4 Templates and why we need it in entity framework?
T4 (Text template transformation toolkit) is template based code generation engine.
It is having the extension of .tt and helps to generate the C# code
You can say the T4 template is heart of EDMX file. It generate the C# code behind it’s an entity and context classes.
There are the files for EDMX will generate as:
11. What is Entity Container in entity framework?
Entity container is the logical grouping for entity set and association set that will exist in CSDL, which is having unique name.
12. What is POCO class in Entity Framework?
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.
13. What is Data first approach?
In database first approach you should have database created with list of tables that you want to store database and with the help of that database you will create your entity framework classes.
Follow the below link to know how to create entity framework data first.
14. What is Entity Graph in entity framework?
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.
15. What is Code first approach?
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.
Follow the below link to see the example of code first approach.
16. What is Model first approach?
In model first approach you will generate the models by using the visual studio.
And create the tables or POCO class with the help of designer instead of your own.
17. Differences between POCO, Model First and data first approach?
1. Database create before generation of code.
2. Automated code generation by visual studio.
3. using of T4 template generate the C# code.
4. No much control over the Code.
Code First Approach:
1. Database generated from POCO classes.
2. No automated code generation.
3. Fully control over the code. you need to write your own C# Code.
Model First Approach:
1. Creation of Context and tables using the Designer tool.
2. T4 template will generate the C# code.
3. You have not full control over the code or DB.
4. After creation of tables, once you run application it generate database.
18. How to do add, update, and delete using EF?
Follow the below link to see the Crud operation in entity framework;
19. How can use stored procedures in Entity Framework?
There are two way to call the store procedure. Please find the below link to check how to call the store procedure.
Or you can call,
var emps = context.Employees.SqlQuery("exec GetEmployeeDetailsnyID @Id =" + 1).ToList();
20. What are the types of property in Entity Framework?
We have three types of property in entity framework.
· Scalar property
· Navigation Property
· Complex Property
21. what is way of loading data in EF (Entity Framework)
Lazy Loading: Process of loading the related objects until it is required.
Eager Loading: Process of loading the related objects is 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.
22. What is lazy loading in Entity Framework?
Lazy loading is the concept of loading the data only when we required. We can say it loading on demand. Suppose you have a Department object there you have Employee object also and there is 1 to many relation between Departments to Employee.
But when you fetch the records of department it won't load the Employee object but the tome when you are doing the foreach on Department object Employee object not loaded but once you will do the iteration for Employee object than only the Employee object will load.
23. How to disable the lazy loading framework?
You can disable the lazy loading by
context.ContextOptions.LazyLoadingEnabled = false;
24. Can we have Enum in Entity Framework?
Yes you can create the Enum in entity framework.
25. What is the difference between DbContext and ObjectContext?
ObjectContext : ObjectContext is responsible for all the Database operations data connection, provide add, Update and delete function.
DbContext : it is Wrapper for ObjectContext and provide the lightweight alternative for object context.
It is useful in Model First and Database First approaches
It is useful in Database First, Model First approach as well as Code First approach.
ObjectContext class is not thread-safe.
DbContext are thread-safe.
ObjectContext used by Entity Framework 4.0 and below.
ObjectContext used by Entity Framework 4.1 and above.
ObjectContext supports Compiled Queries.
DbContext does not support Compiled Queries.
26. How to handle concurrency in Entity Framework.
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.
you should get an OptimisticConcurrencyException error.
27. What is client wins and store wins mode in Entity Framework concurrency?
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.
28. What are scalar and navigation properties in Entity Framework?
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.
29. What are complex types in Entity Framework?
There is a situation when we need to use the common property in many tables. To make and to work with the common property we make it as Complex property. Like in Below Screen we make Address and Phone Number as Complex Property.
Now the complex property will display like.
30. Entity framework performance tips.
There are list of item which will help to improve the Entity Frame work performance like.
a. Using of projection query : Instead of fetching all the records from the database, fetch only those which is required. e.g. If you have customer object and you want UserName, Password and Phone Number so Query it like.
var posts = context.Customer.Select(p => new
Id = p.UserName,
Title = p.Password,
Phone = p.Phone
b. Disable Change Tracking if not required : Suppose if you are fetching the records to view not for any modification purpose so you can disable object tracking from merge Option. Like below code.
using (var context = new CompanyDetailsEntities())
var emps = context.Employees.AsNoTracking().Where(o => o.EmployeeID == 1);
c. Pre-Generating Views to reduce response time for first request : If there are huge database and creating lot of view to show the records, view are C# code which is interacting with the database to load. If generating all the view in one instance and in first call, than performance will degrade to avoid it use T4 template and EdmGen.exe command-line tool.
d. Use Index : Make sure you have added Indexes to entities. You can add the index to entities when using the code first by calling CreateIndex.
e. Use Compiled Query : Use Compiled query if you are hitting the database frequently. It Reduce the turnaround time.
IQueryable lstEmp = from o in context.Employees
where o.EmployeeID == 1