Enitiy framework Flashcards

1
Q

What is Entity Framework?

A

ADO.NET entity 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the benefits of using EF?

A

The main and the only benefit of EF is it auto-generates code for the Model (middle layer), Data Access Layer, and mapping code, thus reducing a lot of development time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the different ways of creating these domain / entity objects?

A

Entity objects can be created in two ways: from a database structure, or by starting from scratch by creating a model.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is pluralize and singularize in the Entity Framework dialog box?

A

“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”)
If you select the below checkbox, Entity Framework generates a naming convention which adheres to plural and singular coding conventions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the importance of EDMX file in Entity Framework?

A

EDMX (Entity Data Model XML) is an XML file which contains all the mapping details of how your objects map with SQL tables. The EDMX file is further divided into three sections: CSDL, SSDL, and MSL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

CSDL (Conceptual Schema definition language) is the conceptual abstraction which is exposed to the application.
SSDL (Storage Schema Definition Language) defines the mapping with your RDBMS data structure.
MSL (Mapping Schema Language) connects the CSDL and SSDL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are T4 templates?

A

T4 (Text Template Transformation Toolkit) is a template based code generation engine. You can go and write C# code in T4 templates (.tt is the extension) files and those C# codes execute to generate the file as per the written C# logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the importance of T4 in Entity Framework?

A

T4 files are the heart of EF code generation. The T4 code templates read the EDMX XML file and generate C# behind code. This C# behind code is nothing but your entity and context classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we read records using Entity Framework classes?

A

In order to browse through records you can create the object of the context class and inside the context class you will get the records.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can we add, update, and delete using EF?

A

Create the object of your entity class, add it to the data context using AddObject method, and then call the SaveChanges method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why does entity framework run slowly?

A

By default EF has lazy loading behavior. Due to this default behavior if you are loading a large number of records and especially if they have foreign key relationships, you can have performance issues. So you need to be cautious if you really need lazy loading behavior for all scenarios. For better performance, disable lazy loading when you are loading a large number of records or use stored procedures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can you explain lazy loading in a detailed manner?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we turn off lazy loading?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can we use stored procedures in Entity Framework?

A

You can use stored procedure mapping details in EDMX as shown in the below figure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are POCO classes in Entity Framework?

A

POCO means Plain Old C# Object. When EDMX creates classes, they are cluttered with a lot of entity tags. For instance, below is a simple customer class generated using Entity Framework. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do we implement POCO in Entity Framework?

A

To implement POCO is a three step process:

Go to the designer and set the code generation strategy to NONE. This step means that you would be generating the classes on your own rather than relying on EF auto code generation.
Now that we have stopped the auto generation of code, we need to create the domain classes manually. Add a class file and create the domain classes like the Customer class we created.
Write your Context layer code inheriting from ObjectContext. This code you can copy paste from the behind code of EF, also before disabling auto-generation.
17
Q

In POCO classes do we need EDMX files?

A

Yes, you will still need EDMX files because the context object reads the EDMX files to do the mapping.

18
Q

What is Code First approach in Entity Framework?

A

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.

19
Q

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

A

EDMX Entity Context
Simple entity framework Needed Auto Auto
POCO approach Needed Manual Auto
Code First

20
Q

How can we do pessimistic locking in Entity Framework?

A

We cannot do pessimistic locking using Entity Framework. You can invoke a stored procedure from Entity Framework and do pessimistic locking by setting the isolation level in the stored procedure. But directly, Entity Framework does not support pessimistic locking.

21
Q

What is client wins and store wins mode in Entity Framework concurrency?

A

Client wins and store wins are actions which you would like to take when concurrency happens. In store wins / database wins, the data from the server is loaded into your entity objects. Client wins is opposite to stored wins, data from the entity object is saved to the database.

22
Q

What are scalar and navigation properties in Entity Framework?

A

Scalar properties are those where actual values are contained in the entities. For example, in the above customer entity, customername and customerid are scalar properties. Normally a scalar property will map to a database field.

Navigation properties help to navigate from one entity to another entity. For instance, consider the below example in which we have two entities: Customer and Address, and a customer has multiple address objects.

Now we would like to have a facility where at any given moment we would like to browse from a given customer object to the addresses collection and from the address object to the customer.

23
Q

What are complex types in Entity Framework?

A

There can be situations where you have common properties across entities. For example, consider the below figure where we have Customer and Supplier entities. They have three fields in common: Address1, Address2, and PhoneNo. These fields have been duplicated both in the Customer and Supplier entities.

So to remove these duplicate and redundant fields, we can move them to a common complex type called Address. Complex types group common fields so that they can be reused across entities.

24
Q

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

A

LINQ to SQL is good for rapid development with SQL Server. EF is for enterprise scenarios and works with SQL Server as well as other databases.
LINQ maps directly to tables. One LINQ entity class maps to one table. EF has a conceptual model and that conceptual model maps to the storage model via mappings. So one EF class can map to multiple tables, or one table can map to multiple classes.
LINQ is more targeted towards rapid development while EF is for enterprise level where the need is to develop a loosely coupled framework.

25
Q

What is the difference between DbContext and ObjectContext?

A

DbContext is a wrapper around ObjectContext, it’s a simplified version of ObjectContext.

As a developer you can start with DbContext as it’s simple to use. When you feel that some of the operations cannot be achieved by DbContext, you can then access ObjectContext from DbContext, as shown in the below code: