Entity Framework – What and Why?
Entity Framework is an open source object relational model framework for the applications built on .NET. It facilitates the developers to work with data without looking into underlying database tables and columns where the data is stored. With Entity Framework in place, developers can now create and maintain data-oriented applications with less code when compared to applications running on traditional ADO.NET code.
Why do we need Entity Framework?
Before the Entity Framework came along, developers had to write ADO.NET code or Enterprise Data Access Block to perform data transitions (CRUD operations) with the respective databases. Often, this process involved opening a connection with the database, getting the data or submitting the data to the database, converting data into .NET object or a .NET object to the dataset. This was a time consuming and error prone process, so Microsoft came up with a framework called the “Entity Framework” which automates all database related activities within the application.
Using the Entity Framework within your application
The following figure illustrates where the entity framework fits into your application.
Different Types of Approaches with Entity Framework
There are three types of approaches you can use while developing your application using Entity Framework:
A little about EF Core
EF Core is a new version and advanced when compared to Entity Framework 6, but this cannot be considered as Entity Framework 7. It is rich in features and yet very simple (meaning simpler than current entity framework versions). It is the extensible and cross platform version of the entity framework, which works well with relational and non-relational databases. Further, it can be used with full Dot Net framework and it also has the ability to go with dot net core and cross platform. EF core uses principle called “pay per play”, meaning one should not pay for complex feature which he/she not using it. As EF6 is very complex, in terms of mappings, it’s very difficult to add features on top of it. EF6 consumes more time in warm up (meaning time consumed in creating the model).
In this approach, we generate the context and entities for the existing database using EDM wizard integrated in Visual Studio or executing EF commands.
EF 6 supports the database-first approach extensively. EF Core includes limited support for this approach.
This approach can be useful when we don’t have existing database for our application. Here we first create domain classes and context classes, later we create database from these classes. Developers who follow Domain Driven Design usually prefer this approach much.
In this approach, we create entities, relationships, and inheritance hierarchies directly on the visual designer integrated in Visual Studio and then generate entities, the context class, and the database script from your visual model.
EF 6 has limited support for this approach & EF core will not support this approach type.
Advantages of Entity Framework
- Provides auto generated code
- Reduces development time
- Reduces development cost.
- Enables developers to visually design models and mapping of database
- Enables programming of conceptual models
Limitations of Entity Framework
- Lazy loading is the main disadvantage
- EF’s logical schema cannot understand the business entities and relation between them
- EF is not applicable for every RDMS
- If we change any schema of the database, it will not work anymore until and unless we update the schema on the solution
- Not suitable for very huge domain models
- It will not get relational mapping between entities when there is any non-clustered index defined on any of the entity. Drop the non-clustered index first and then update the model in order get relational mappings in place.
Exploring new technical stuff and trying to incorporate them in my work domain wherever possible. - That is me. A "Performace Guru" who loves to optimize modules for the best outcomes. Movies and infotainment channels to relax and travelling with family.All stories by: Rakesh Kanduri