Development Approach with Entity Framework
In this article, I am going to discuss Development Approach with Entity Framework. The entity framework provides three different approaches when working with the database and data access layer in your application as per your project requirement. These are
We can use the Database First approach if the database schema already existing. In this approach, we generate the context and entities for the existing database using the EDM wizard. This approach is best suited for applications that use an already existing database.
Database First approach is useful:
- When we are working with a legacy database
- If we are working in a scenario where the database design is being done by another team and the application development starts only when the database is ready
- When we are working on a data-centric application
You can use the Code First approach when you do not have an existing database for your application. In the code-first approach, you start writing your entities (domain classes) and context class first and then create the database from these classes using migration commands.
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.
That means Developers who follow the Domain-Driven Design (DDD) principles, prefer to begin coding with their domain classes first and then generate the database required to persist their data.
Code First approach is useful:
- If there is no logic is in the database
- When full control over the code, that is, there is no auto-generated model and context code
- If the database will not be changed manually
This approach is very much similar to the Code First approach, but in this case, we use a visual EDMX designer to design our models. So in this approach, we create the entities, relationships, and inheritance hierarchies directly on the visual designer and then generate entities, the context class, and the database script from your visual model.
Note: The visual model will give us the SQL statements needed to create the database, and we can use it to create our database and connect it up with our application.
The Model First approach is useful:
- When we really want to use the Visual Entity Designer
Choosing the Development Approach for Your Application:
The below flowchart explaining which is the right approach to develop your application using Entity Framework?
As per the above diagram, if you have an existing database, then you can go with Database First approach because you can create an EDM from your existing database.
But if you don’t have an existing database but you have an existing application with domain classes then you can go with code first approach because you can create a database from your existing classes. But if you don’t have either existing database or domain model classes then you can go with the model first approach.
In the next article, I am going to discuss the DB First approach of Entity Framework. Here, in this article, I try to explain the Development Approach with Entity Framework. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.