Introduction to Code First

The Entity Data model (EDM) is at the heart of the entity framework. We looked at the Entity data model in our last tutorial Basics of Entity Framework. The Entity model describes the conceptual model of our domain objects. The Entity Framework has two ways to work with the Entity Data Model.

  1. Using Visual Designer
    The models are created using the Entity Data Model Designer
  2. Code First
    The Models are created manually

Using Visual Designer

Entity Data Model Designer (EDMX) is a tool that enables application developers to visually design the entity data model (EDM) in the designer. The Entity Data Model is the conceptual model of the data which we discussed in the last tutorial. Using the EDMX tool you can easily create or modify entities,  create associations between entities, etc.  The EDMX tool generates a.EDMX file where all the information about the model is stored in an XML format.

The EDMX tool has various wizards which help you to create the EDMX file from the existing database, Update/Create the existing database.

There are two ways you can create the Entity data model (EDM) using the designer

  1. Model First
  2. Database first

Model First

In this approach, database models are created First. The models are created using EDMX (Entity Data Model Designer) Designer.  Entity Framework then uses the .EDMX, which is generated by the tool to generate the database from the model. It also creates the data access code to manipulate it

Database First

In this approach, EDMX is created from the existing database. This is the reverse of the model first approach where models are created first and database later. The Entity Data Model can be updated whenever database schema changes. Also, database-first approach supports stored procedure, view, etc.

Database first is used if you have Database is developed separately or if you have an existing DB.  Any changes made to the database can be easily updated in the model from the database.

Database first/model first is going to be retired in Entity Framework Version 7. Here is the link to to the MSDB blog

Code First

In the Code First approach, you manually create the entity domain model (EDM) classes in your application. The database is created from these classes. This gives you the direct control of what is being generated in the database. The Visual model designer is not used in the code first approach. Code First follows the Domain-Driven Design (DDD) principles

Benefits of Code First

Code first gives full control over the code to the developer. There is no auto-generated code as in the case of the database first or mode first.  Modifying the auto-generated codes are risky and may break your code. The changes may get overwritten when you rebuild the model.

In code first, you are more responsible for the database schema. You have to configure the relationships, constraints, and associations between entities. You need to specify the database column sizes.

Adding a custom field in auto-generated models, you may need to extend the model class. This is not a problem in the code first as you can add it to the existing class.

Code first migrations allow you to easily create the migration script and apply it to the database. With every change made to the database, you can generate the new migration script, which is timestamped. It also has features to roll down the migration

Code first can be used in both existing or new databases. In case of an existing database, you may have to manually code model classes to match the database. The database first approach will create all the domain classes for you.

Entity Framework 7 Drops EDMX

The database first and model first has no real differences. Generated code is the same and you can combine these approaches. For example, you can create database using designer, then you can alter database using SQL script and update your model.

The difference between database first/model first and code first is the.EDMX file. If you choose the database first/model first, the EF uses the.EDMX file to generate an in-memory model of the database.  In case of  Code First in-memory model of the database is created from the POCO classes.  Once the in-memory model is created then EF will work in the same way irrespective of how these models are created.

In the EF 7 .EDMX file is discarded. Hence code the first workflow is the only way going forward.

Conclusion

The Entity framework allows us to create the model directly using the code, which is called Code First. In the next tutorial, we will see how to create a simple project using code first workflow.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top