Prerequisites: – For Best understanding of entity framework basic knowledge of .Net Framework, C#, Visual Studio and MS SQL Server is required.
We will use the latest version of entity framework 6 at the time. Install the following tools to work with entity framework:
- .NET Framework 4.5
- Visual Studio 2012
- Entity Framework
- MS SQL Server 2005/2008/2012 Express or SQL CE
Writing and managing ADO.Net code for data access is tedious and monotony job. Microsoft has provided an ORM framework called “Entity Framework” to automate database related activities for your application.
Object Relational Mapping (ORM)
ORM is a tool for storing data from domain objects to relational database like MS SQL Server, in an automated way without much programming.
- Domain class objects
- Relational database objects
- Mapping information on how domain objects map to relational database objects (tables, views & stored procedures).
ORM allows us to keep our database design separate from our domain class design. This makes the application maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update and Delete) so that the developer doesn’t need to write it manually.
The Microsoft ADO.NET Entity Framework is an Object Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write.
Entity Framework using LINQ
Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects. The Entity Framework’s ORM implementation provides 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.
Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.
Entity framework is useful in three scenarios-
First, if you already have existing database or you want to design your database first than other parts of the application.
Second, you want to focus on your domain classes and then create the database from your domain classes.
Third, you want to design your database schema on the visual designer and then create the database and classes.
As per the above figure, EF creates data access classes for our existing database, so that we can use these classes to interact with the database instead of ADO.Net directly.
EF can also creates the database from our domain classes, thus we can focus on our domain driven design.
EF provides a model designer where we can design our DB model and then EF creates database and classes based on db model.