Creating Model First with Entity Framework

Dheeraj Kumar Gunti

Reading Time : ( words)

The Entity Framework is an object-relational mapping framework or O/RM. That is, it has the ability to map objects and their state to a relational data store. The principle advantage of an O/RM is that it provides synchronization between the data access layer and the data storage provider, most typically a relational database. This synchronization is possible because EF generates both the database schema and the data access layer code to work with it. Entities contain properties that are mapped to fields inside tables in a relational database. If you already have a database you can let EF automatically generate the model for you. However, if you don’t already have the database you can define the model, and let EF generate the database schema for you. This is known as “Model-First” development. EF4 significantly improves on one particular feature in EF1, model-first – the ability to start with a conceptual model and create the database from it. Following is the example for creating Model first application:

1. Create a New Project and select an empty web application.

2.Add a new Entity Data Model.

3. Choose Empty Model in the wizard.

4. Once the model is created, then user can see an empty entity designer.

5. Right click on the entity designer to add new entities.

6. Once the entity is created, you can add up 'n' number of properties to that entity.

7. You can modify property data types as below.

8. Once the entity is done. Right click on the designer and choose Generate Database from Model.

9. Choose new database connection. Specify name of the database which you want to create. If you are using sql server then change the datasource to sql server.

10. Once you click on "ok", then user will get a message prompt which states database doesn't exists would you like to create.

11. Once the database is created, Generate Database Wizard will generate DDL Script.

12. Execute the DDL Script to create schema.