- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Entity Framework - Code First Approach
The Entity Framework provides three approaches to create an entity model and each one has their own pros and cons.
Code First
Database First
Model First
In this chapter, we will briefly describe the code first approach. Some developers prefer to work with the Designer in Code while others would rather just work with their code. For those developers, Entity Framework has a modepng workflow referred to as Code First.
Code First modepng workflow targets a database that doesn’t exist and Code First will create it.
It can also be used if you have an empty database and then Code First will add new tables to it.
Code First allows you to define your model using C# or VB.Net classes.
Additional configuration can optionally be performed using attributes on your classes and properties or by using a fluent API.
Why Code First?
Code First is really made up of a set of puzzle pieces. First are your domain classes.
The domain classes have nothing to do with Entity Framework. They re just the items of your business domain.
Entity Framework, then, has a context that manages the interaction between those classes and your database.
The context is not specific to Code First. It s an Entity Framework feature.
Code First adds a model builder that inspects your classes that the context is managing, and then uses a set of rules or conventions to determine how those classes and the relationships describe a model, and how that model should map to your database.
All of this happens at runtime. You ll never see this model, it s just in memory.
Code First also has the abipty to use that model to create a database if you wanted to.
It can also update the database if the model changes, using a feature called Code First Migrations.
Environment Setup
To start working with EF Code First approach you need the following tools to be installed on your system.
Visual Studio 2013 (.net framework 4.5.2) or later version.
MS SQL Server 2012 or latter.
Entity Framework via NuGet Package.
Install EF via NuGet Package
Step 1 − First, create the console apppcation from File → New → Project…
Step 2 − Select Windows from the left pane and Console Apppcation from the template pane.
Step 3 − Enter EFCodeFirstDemo as the name and select OK.
Step 4 − Right-cpck on your project in the solution explorer and select Manage NuGet Packages…
This will open NuGet Package Manager, and search for EntityFramework. This will search for all the packages related to Entity Framework.
Step 5 − Select EntityFramework and cpck on Install. Or from the Tools menu cpck NuGet Package Manager and then cpck Package Manager Console. In the Package Manager Console window, enter the following command: Install-Package EntityFramework.
When the installation is complete, you will see the following message in the output window “Successfully installed EntityFramework 6.1.2 to EFCodeFirstDemo”.
After installation, EntityFramework.dll will be included in your project, as shown in the following image.
Now you are ready to start working on Code First approach.
Advertisements