What is Entity Framework?
Entity Framework is an Object Relational Mapper (ORM) from Microsoft that lets the application’s developer’s work with relational data as business models. It eliminates the need for most of the plumbing code that developers write for data access.
Entity Framework provides a comprehensive, model-based system that makes the creation of a data access layer very easy for the developers by freeing them from writing similar data access code for all the domain models. Current version Is 6.0.
Here are a few benefits of using Entity Framework:
- The development time is reduced since the developers don’t have to write all
- the ADO.NET plumbing code needed for data access
- We can have all the data access logic written in a higher-level language such
- as C# rather than writing SQL queries and stored procedures
- Since the database tables cannot have advanced relationships (inheritance) as
- the domain entities can, the business model, that is, the conceptual model can
- be used to suit the application domain using relationships among the entities.The underlying data store can be replaced relatively easily if we use an ORM since all the data access logic is present in our application instead of the data layer. If an ORM is not being used, it would be comparatively difficulty do so.
Entity Framework relies on the conceptual data model for all its working ( Entity Data Model [EDM). Let’s understand what Is EDM means.
EDM is nothing but conceptual data model classes. EDM defines out conceptual model classes, relationship between those classes and the mapping of those models to the database schema.
Once EDM ready, we can perform the CRUD operations using model objects. First we have to understand ObjectContext class.
ObjectContext class responsible for:
- Managing database connection
- Providing support to perform CRUD operations
- Keeping track of changes in the models so that the models can be updated in the databse.
Now , in order to create EDM, there are different styles and scenarios.
Database First: This is the approach that will be used with an existing database schema. In this approach, the EDM will be created from the database schema. This approach is best suited for applications that use an already existing database.
Code first: This is the approach where all the domain models are written in the form of classes. These classes will constitute our EDM. The database schema will be created from these models. This approach is best suited for applications that are highly domain-centric and will have the domain model classes created first. The database here is needed only as a persistence mechanism for these domain models.
Model First: This approach is very similar to the Code First approach, but in this case, we use a visual EDM designer to design our models. The database schema and the classes will be generated by this conceptual model. The model will give us the SQL statements needed to create the database, and
we can use it to create our database and connect up with our application. For all practical purposes, it’s the same as the Code First approach, but in this approach, we have the visual EDM designer available.
Which approach to choose depends on this small check list, if the answer to any question is yes, then that approach should be used even without looking at the next questions.
Question | Approach |
Is there a legacy database or does the database already exist? | Database First |
Will we be getting a database created by the DBAs before starting the development? | Database First |
Will there be frequent database changes, based on which our application should change? | Database First |
Do we want to use the Visual Entity Designer to generate the database and model classes ? | Model First |
Do we have the model classes already and we need the database to save the data only ? | Code First |
Do we want to write all the model classes, implement them, and then think about the database storage later ? | Code First |
We don’t want to deal with the auto – generated classes and would prefer to write them ourselves | Code First |
Is the answer to all the preceding questions “no” ? | Database First |