You are currently viewing 9. Entity Framework, Linq, Lambda Expressions

9. Entity Framework, Linq, Lambda Expressions

What is Entity Framework?

Entity Framework is Microsoft’s latest and recommended way for data access for new applications. EF 4.1 enhances the offering by adding support for Code First model and DbContext API’s.The EDM addresses the challenges that arise from having data stored in many forms. For example, consider a business that stores data in relational databases, text files, XML files, spreadsheets, and reports. This presents significant challenges in data modeling, application design, and data access.

The Entity Data Model (EDM) uses three key concepts to describe the structure of data: entity type, association type, and property. These are the most important concepts in describing the structure of data in any implementation of the EDM.

 

  1. Entity Type: The entity type is the fundamental building block for describing the structure of data with the Entity Data Model. In a conceptual model, entity types are constructed from properties and describe the structure of top-level concepts, such as a customers and orders in a business application.

 

  1. Association Type: An association type (also called an association) is the fundamental building block for describing relationships in the Entity Data Model. In a conceptual model, an association represents a relationship between two entity types (such as Customer and Order).

 

  1. Property: Entity types contain properties that define their structure and characteristics. For example, a Customer entity type may have properties such as CustomerId, Name, and Address.

 

 

Using Code First model, how can I mark a field/property as the primary key if it does not follow the code first convention?

 

In our case above, EF looks for the word “ID” with a combination with the entity name (e.g. Project) to determine both the EntityKey and the primary key. If we rename the “Id” to say “UniqueProjectIdentifier”, we will need to decorate that property with the KeyAttribute ([Key]) to make it all work.

 

 

 

What is .edmx file and what does it contain?

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.

 

 

What is LINQ To Entities?

LINQ to Entities provides Language-Integrated Query (LINQ) support for querying entities.

LINQ to Entities enables developers to write queries against the database using one of the supported .NET Framework programming languages such as Visual Basic or Visual C#.

 

What is Deferred Loading (Lazy Loading)?

When objects are returned by a query, related objects are not loaded at the same time.

Instead they are loaded automatically when the navigation property is accessed. Also known as “lazy loading,”

 

What is Eager Loading?

The process of loading a specific set of related objects along with the objects that were explicitly requested in the query.

 

Will there be any issues adding a table without primary keys to a data model?

Every entity must have a key, even in the case where the entity maps to a view. When you use the Entity Designer to create or update a model, the classes that are generated inherit from EntityObject, which requires EntityKey. So, we have to have a primary key in the table to add it to the data model.

 

What would be the extension of the EDM file?

Answer:  edmx

 

Is it possible to create an entity model without a pre-existing database?

Answer:  Yes, it is possible to create an entity model without a pre-existing database and then generating a database from the model using the Entity Framework.

 

10: True or False: With Entity Framework the queried data is returned as rows and columns or data table records.

Answer:  False; Data is retrieved as objects.

 

How Entity Framework manages entities that do not inherit from the EntityObject?

Answer:  It manages these objects as POCO (Plain Old CLR objects) entities.

 

What is Disconnected scenario means?

Disconnected scenario is when an entity is retrieved from the database and modified in the different context. Disconnected scenario is complex because context doesn’t know anything about modified entity so you have to tell to ObjectContext that what has changed in entity.

 

DB Transactions


using (EntitiesContext context = new EntitiesContext())  
{  
    using (var transaction = context.Database.BeginTransaction())  
    {  
        try  
        {  
            EmployeeMaster employee = new EmployeeMaster();  
            employee.Code = "A0001";  
            employee.Name = "Jignesh Trivedi";  
            employee.DepartmentId = 1;  
            context.Employees.Add(employee);  
            context.SaveChanges();  
  
            DepartmentMaster dept = new DepartmentMaster();  
            dept.Code = "DEP0001";  
            dept.Name = "Department 1";  
            context.Departments.Add(dept);  
            context.SaveChanges();  
  
            transaction.Commit();  
        }  
        catch (Exception ex)  
        {  
            transaction.Rollback();  
        }  
    }  
}