Getting Started with EF6, Code First
Presenter: Tanveer Singh, Mindfire Solutions
1. Entity Framework CF First Approach
2. Basics of Code First
3. Fluent Api
4. Data Annotations
What is Code First ?
Code First development with Entity Framework 6.0
New approaches to modeling for Entity Framework 6.0.
1. Model First
2. Code First
3. Database first
Model First -
Using the Model First approach a developer can start working with the model of the database and
creating entities which make logical sense irrelevant of how they will actually be stored in the
database in terms of tables.
Code First -
The Code first approach is the most recent approach provided by Microsoft as part of the Entity
Framework 4.0. Its a more developer oriented approach where a developer can literally code the
entities required within the system as if they were regular classes and objects and then create the
database schema from these classes by defining special relationships and mappings within these
The main advantages in utilizing the Code First approach is that the developer has complete
control on the relations between the entities Using the code first approach the developer can also
provide restrictions and annotations on specific properties in the domain models (classes) which
will serve as validations both on the database and also within the code itself.
Database First -
The Database First Approach
provides an alternative to
the Code First and Model
First approaches to the
Entity Data Model and it
creates model codes
DbContext etc.) from the
database in the project and
those classes become the
link between the database
One important thing to understand is that there are -
Two new types introduced for Code First approach
DbContext and DbSet.
DbContext is a simplified alternative to ObjectContext and is the primary object for interacting with a
database using a specific model.
DbSet(Of TEntity) is a simplified alternative to ObjectSet(Of TEntity) and is used to perform CRUD
operations against a specific type from the model in Code First approach.