Click here to Skip to main content
15,887,596 members
Articles / Web Development / HTML

Entity Framework Basics

Rate me:
Please Sign up or sign in to vote.
4.89/5 (21 votes)
21 Aug 2014CPOL6 min read 44.7K   55   4
Entity Framework basics

When was Entity Framework Released?

The first version of Entity Framework 3.5 was released along with Visual Studio 2008 SP1 and .NET 3.5 SP1.
The second version (also the current version) of Entity Framework i.e. 4.0 was released along with VS 2010 and .NET 4.0.

Before the release of Entity framework 3.5, there were many database access technologies like DAO (Data Access Objects), RDO (Remote Data Objects), then ADO (ActiveX Data Objects) and then finally ADO.NET.

Although ADO.NET working with the DataSet and DataReader has served our data access needs, a developer needs to spend a lot of time being concerned with the details of the schema, trying to keep up with database schema changes and doing redundant tasks while interacting with the database, over and over again.

Image 1

Image taken from http://www.entityframeworktutorial.net/EntityFramework5/entity-framework5-introduction.aspx.

What is Entity Framework?

  1. The ADO.NET Entity Framework (EF) is an Object/Relational mapping (ORM) framework and is a set of technologies in ADO.NET, for developing applications that interact with data.
  2. ADO.NET developer spends a lot of time keeping up with the database changes whereas EF provides a mapping from the relational database schema to the objects and offers an abstraction of ADO.NET.
  3. So with EF, you can define Entity classes that are independent of a database structure and then map them to the tables and associations of the database.
  4. Since we are now working with Entities which have their own schema, we are shielded from the changes in the database. The object context keeps tabs on the entities that are changed.
  5. In simple words, with the Entity Framework, you are architecting, designing and developing at a conceptual level. You are no more worried about the ‘specific details’ of communicating with the database and switching from one relational database to the other is also possible with EF, without much efforts.
  6. The Entity Framework’s ORM implementation provides many more services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.
    To view more details about change tracking, identity resolution, lazy loading, and query translation, just visit the below link: http://www.develop.com/entityframework4.
  7. It basically generates entities according to the database tables and provides the capability to perform basic create, update, read, delete operations and also plays an important role to manage the 1 to 1, 1 to many OR many to many relations.

What is the Role of ADO.NET Now?

  • We can't say that EF is released to replace ADO.NET.
  • Actually, we can say that EF is an enhancement to ADO.NET and it eliminates the gap between the application and the database.
  • In fact behind the scenes, EF uses ADO.NET classes, but the details are abstracted from you.
  • EF provides a shift from Database-oriented (DataReader, DataSet) to Model-oriented development.
  • So, instead of focusing on a database, a developer focuses on the Entity that represents the Business Model of the application.

Is EF an Alternative to ADO.NET?

The answer would be "yes and no".
Yes, because the developer will not be writing ADO.NET methods and classes for performing data operations and
NO because this model is actually written on top of ADO.NET, meaning under this framework, we are still using ADO.NET.

The detailed folder/file structure of EF?

Image 2

There are mainly 3 files in EF:

  • CSDL- Conceptual schema definition language
  • SSDL - Store schema definition language
  • MSL - Mapping specification language

These are XML-based languages that respectively describe the conceptual model, storage model, and the mapping between these models.

In an Entity Framework application, model and mapping metadata are loaded from three files with extensions .csdl, .ssdl, and .msl.

When Are They Generated?

The Entity Data Model Designer (Entity Designer) stores model and mapping information in an .edmx file at design time. At build time, the Entity Designer uses the information in an .edmx file to create the .csdl, .ssdl, and .msl files that are needed by the Entity Framework at runtime.

Where Are They Located?

By default, these files are included in the binary file located in the output under bin folder.
We are not able see the physical files.
But we can also output CSDL, SSDL, and MSL Files as separated files.

Image 3

There are mainly three approaches to create .edmx in the project:

  1. Database First Approach
  2. Model First Approach
  3. Code First Approach

I will explain Database First Approach in this article.

How to Add Entity Framework(.edmx) File in Solution

  1. Select the project. Right click on the project. Select Add New Item.

    Image 4

    Image 5

    Image 6

  2. Give your desired name to .edmx project
  3. Select generate model from database. As we are following database first approach, so database is already created and we will generate the model from database.

    Image 7

  4. Choose the data connection from which you want to create the model.
  5. Give desired name to the entity connection string that will be used in web.config.

    Image 8

  6. Click on next and select tables OR Stored Procedures OR Views which you want to import in the model.

    Image 9

  7. Click on finish, your entity framework model will be created.

    Image 10

How to See the Model Browser

  1. Right click in the model,and select model browser.

    Image 11

  2. You will be able to see the model browser sections.

    Image 12

  3. If you have changed the stored procedures in the database, then you need to update the model so that the stored procedures in the database will get reflected in model.

    Image 13

  4. If you want to include the stored procedures OR tables those are changed in the database--->Select Refresh and select the stored procedures which have changed.
  5. If you want to add newly added stored procedures OR tables OR Views in model--->select Add tab and select the corresponding entities.
  6. If you want to delete the entities from model---> select the delete tab and select the corresponding entities.

    Image 14

How to Add Function Import

  1. In the EntityContainer, right click on the function import and select Add Function Import.

    Image 15

  2. Type the desired Function Import Name.

    Image 16

  3. Select the stored procedure name from the drop down.

    Image 17

  4. You need to decide the return type of the created function import. In order to decide the return type, you have to click on the Get Column Information.
  5. This will display the list of columns returned by that particular stored procedure.
  6. Here, if the stored procedure is returning multiple columns, then you need to create the complex type, so click on create New Complex Type.
  7. Give proper name to the complex type as this will be used as the return type of the created function import.

Image 18

Image 19

Image 20

Image 21

So our model is created with the desired database. We have also created one function import. This function import will be used in data access layer for accessing the data.

I will explain the remaining approaches in subsequent articles.
Comments and suggestions are most welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood one. Pin
Imran Abdul Ghani22-Sep-14 20:16
Imran Abdul Ghani22-Sep-14 20:16 
Nice details provided about Entity Framework.
For building ASP.NET MVC application with Entity Framework, please follow here
GeneralMy vote of 5 Pin
Humayun Kabir Mamun22-Aug-14 3:25
Humayun Kabir Mamun22-Aug-14 3:25 
QuestionMy vote 5 Pin
Nitin S22-Aug-14 0:08
professionalNitin S22-Aug-14 0:08 
GeneralMy vote of 4 Pin
Bhushan Panhale21-Aug-14 18:33
Bhushan Panhale21-Aug-14 18:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.