Enitiy framework Flashcards
What is Entity Framework?
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.
What are the benefits of using EF?
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.
What are the different ways of creating these domain / entity objects?
Entity objects can be created in two ways: from a database structure, or by starting from scratch by creating a model.
What is pluralize and singularize in the Entity Framework dialog box?
“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.
What is the importance of EDMX file in Entity Framework?
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.
Can you explain CSDL, SSDL and MSL sections in an EDMX file?
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.
What are T4 templates?
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.
What is the importance of T4 in Entity Framework?
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 can we read records using Entity Framework classes?
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 can we add, update, and delete using EF?
Create the object of your entity class, add it to the data context using AddObject method, and then call the SaveChanges method.
Why does entity framework run slowly?
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.
Can you explain lazy loading in a detailed manner?
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 can we turn off lazy loading?
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 can we use stored procedures in Entity Framework?
You can use stored procedure mapping details in EDMX as shown in the below figure.
What are POCO classes in Entity Framework?
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.