Development Approach with Entity Framework

Development Approach with Entity Framework

In this article, I will discuss Development Approach with Entity Framework. 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

  1. Database-First
  2. Code-First
  3. Model-First
Database-First Approach:

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.

Development Approach with Entity Framework

Database First approach is useful:

  1. When we are working with a legacy database
  2. 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
  3. When we are working on a data-centric application
Code-First Approach:

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.

Development Approach with Entity Framework

Code First approach is useful:

  1. If there is no logic is in the database
  2. When full control over the code, that is, there is no auto-generated model and context code
  3. If the database will not be changed manually
Model-First Approach:

This approach is very much similar to the Code First approach, but in this case, we use a visual EDM 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 up with our application.

Development Approach with Entity Framework

The Model First approach is useful:

  1. 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?

Development Approach with 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 will discuss the DB First approach of Entity Framework.

SUMMARY

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.

Leave a Reply

Your email address will not be published. Required fields are marked *