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 taken from http://www.entityframeworktutorial.net/EntityFramework5/entity-framework5-introduction.aspx.
What is Entity Framework?
- 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.
- 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.
- 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.
- 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.
- 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.
- 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. - 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?
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.
There are mainly three approaches to create .edmx in the project:
- Database First Approach
- Model First Approach
- Code First Approach
I will explain Database First Approach in this article.
How to Add Entity Framework(.edmx) File in Solution
- Select the project. Right click on the project. Select Add New Item.
- Give your desired name to .edmx project
- 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.
- Choose the data connection from which you want to create the model.
- Give desired name to the entity connection string that will be used in web.config.
- Click on next and select tables OR Stored Procedures OR Views which you want to import in the model.
- Click on finish, your entity framework model will be created.
How to See the Model Browser
- Right click in the model,and select model browser.
- You will be able to see the model browser sections.
- 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.
- 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.
- If you want to add newly added stored procedures OR tables OR Views in model--->select Add tab and select the corresponding entities.
- If you want to delete the entities from model---> select the delete tab and select the corresponding entities.
How to Add Function Import
- In the
EntityContainer
, right click on the function import and select Add Function Import.
- Type the desired Function Import Name.
- Select the stored procedure name from the drop down.
- 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.
- This will display the list of columns returned by that particular stored procedure.
- Here, if the stored procedure is returning multiple columns, then you need to create the complex type, so click on create New Complex Type.
- Give proper name to the complex type as this will be used as the return type of the created function import.
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.