Entity framework is an open source ORM.ORM stands for Object Relational Mapper.ORM allows us to work with relational databases such as SQL Server and Oracle using strongly typed objects.
If we are not using any ORM in our application then to work with relational databases we need to write ADO.NET code.While directly working with ADO.NET we always need to follow few steps such as:
- Create and open connection to the database.
- Create and execute a command object.This will execute a Query to retrieve or update data in the database table.
- Find the relevant information in the data returned by the query
- Close the connection object created in step 1.
We need to follow the above steps even if we want to retrieve only a single record from the table.
ORM such as Entity framework frees us from writing the redundant data access code.Instead we can work with strongly typed objects in the code.We can update and create objects and the persistence of the objects in the database is taken care by the Entity Framework.
So if we have an object representing an Order entity then instead of creating query to retrieve and update the Order in the database we can work with the Order objects in our code.Entity Framework works as an interface between our domain objects and the relational database objects such as tables ,views and stored procedures.
The architecture of Entity Framework allows it to act as an interface between domain objects in our application and the database objects.We can represent the architecture of Entity Framework as:
Entity Data Model or EDM consists of 3 parts
Conceptual Model Consists of classes which represents our domain model
Storage Model Consists of database objects such as tables,views and stored procedures
Mapping Information Maps the conceptual model with the domain model.It is because of this mapping information that Entity Framework is able to identify how to represent the model classes in the database.
LINQ to Entities is LINQ provider which enables us to query the entities returned by the entity framework.We can write LINQ queries instead of working with SQL.The LINQ queries which we write are translated by the Entity Framework into the
SQL queries.
ADO.NET Data Provider This is the actual interface by which Entity Framework communicates with the database
Services Entity Framework provides services such as change tracking,CRUD operations ,lazy loading of entities.
We can easily implement CRUD functionality using Entity Framework.In the below example we are adding and removing an Order in the orders collection:
context.Orders.Add(new Order() { OrderId=1 }); context.Orders.Remove(orders.ElementAt<Order>(0));
In the above code we are writing ado.net code but rather directly adding and removing the Order object in the Orders collection.
Leave a Reply