This is continuation part to the Introduction to EntityFramework
In this article I will be describing the three approaches to using Entity Framework (EF) – Model First, Database First and Code First.
Entity Framework simplifies data access in your application by enabling you to write code to perform CRUD (Create, Read, Update and Delete) operations sans the need of interacting with the underlying database provider directly. There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First. This article discusses all these three approaches and their pros and cons.
In the sections that follow, we will explore each of the three approaches to modeling entities using Entity Framework.
The downside to this approach is that any changes to the underlying database schema would be lost; in this approach your code defines and creates the database. The Code First approach allows you to use Entity Framework and define the entity model sans the designer or XML files. You can use the POCO (Plain Old CLR Objects) approach to define the model and generate your database.
OK, but when should you use the Model First approach? Well, if neither the domain classes nor the database is ready and you would rather define the data model using a visual designer, this approach is for you. However, like in the Code First approach, in the Model First approach manual changes to the database would be lost as the model defines the database.
Source: Msdn |
In this article I will be describing the three approaches to using Entity Framework (EF) – Model First, Database First and Code First.
Entity Framework simplifies data access in your application by enabling you to write code to perform CRUD (Create, Read, Update and Delete) operations sans the need of interacting with the underlying database provider directly. There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First. This article discusses all these three approaches and their pros and cons.
In the sections that follow, we will explore each of the three approaches to modeling entities using Entity Framework.
Code First
The Code First approach helps you to create the entities in your application by focusing on the domain requirements. In essence, you can follow Domain Driven Design (DDD) using this approach. Once your entities have been defined and the configurations specified, you can create the database on the fly using both. The Code First approach gives you more control over your code -- you don't need to work with auto generated code anymore. I like this approach as this gives you a lot of flexibility and control. If you have the domain classes ready, I would always prefer this approach as you can easily create your database from the domain classes.The downside to this approach is that any changes to the underlying database schema would be lost; in this approach your code defines and creates the database. The Code First approach allows you to use Entity Framework and define the entity model sans the designer or XML files. You can use the POCO (Plain Old CLR Objects) approach to define the model and generate your database.
Database First
You can use the Database First approach if the database is already designed and is ready. In this approach, the Entity Data Model (EDM) is created from the underlying database. As an example, you use the database first approach when you generate the edmx files in the Visual Studio IDE from the database. Manual changes to the database is possible easily and you can always update the EDM if need be (for example, if the schema of the underlying database changes). To do this, simply update the EDM from the database in the Visual Studio IDE.Model First
In the Model First approach you can create the EDM first, then generate the database from it. You would typically create an empty EDM using the Entity Data Model Wizard in Visual Studio, define the entities and their relationships in Visual Studio, then generate the database from this defined model. You can easily create entities and define their relationships and associations in the designer in Visual Studio. You can also specify the Key property and the data types for the properties for your entities using the designer. You can use partial classes to implement additional features in your entities.OK, but when should you use the Model First approach? Well, if neither the domain classes nor the database is ready and you would rather define the data model using a visual designer, this approach is for you. However, like in the Code First approach, in the Model First approach manual changes to the database would be lost as the model defines the database.
Post a Comment