- Entity Framework Tutorial
- Entity Framework - Home
- Entity Framework - Overview
- Entity Framework - Architecture
- Entity F - Environment Setup
- Entity Framework - Database Setup
- Entity Framework - Data Model
- Entity Framework - DbContext
- Entity Framework - Types
- Entity Framework - Relationships
- Entity Framework - Lifecycle
- Entity F - Code First Approach
- Entity F - Model First Approach
- Entity F - Database First Approach
- Entity Framework - DEV Approaches
- Entity F - Database Operations
- Entity Framework - Concurrency
- Entity Framework - Transaction
- Entity Framework - Views
- Entity Framework - Index
- Entity F - Stored Procedures
- Entity F - Disconnected Entities
- Entity F - Table-Valued Function
- Entity Framework - Native SQL
- Entity Framework - Enum Support
- Entity F - Asynchronous Query
- Entity Framework - Persistence
- Entity F - Projection Queries
- Entity F - Command Logging
- Entity F - Command Interception
- Entity Framework - Spatial Data Type
- Entity Framework - Inheritance
- Entity Framework - Migration
- Entity Framework - Eager Loading
- Entity Framework - Lazy Loading
- Entity Framework - Explicit Loading
- Entity Framework - Validation
- Entity Framework - Track Changes
- Entity Framework - Colored Entities
- Entity F - Code First Approach
- Entity Framework - First Example
- Entity Framework - Data Annotations
- Entity Framework - Fluent API
- Entity Framework - Seed Database
- Entity F - Code First Migration
- Entity F - Multiple DbContext
- Entity F - Nested Entity Types
- Entity Framework Resources
- Entity Framework - Quick Guide
- Entity Framework - Useful Resources
- Entity Framework - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Entity Framework - Database First Approach
In this chapter, let us learn about creating an entity data model with Database First approach.
The Database First Approach provides an alternative to the Code First and Model First approaches to the Entity Data Model. It creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller.
The Database First Approach creates the entity framework from an existing database. We use all other functionalities, such as the model/database sync and the code generation, in the same way we used them in the Model First approach.
Let’s take a simple example. We already have a database which contains 3 tables as shown in the following image.
Step 1 − Let’s create a new console project with DatabaseFirstDemo name.
Step 2 − To create the model, first right-click on your console project in solution explorer and select Add → New Items…
Step 3 − Select ADO.NET Entity Data Model from middle pane and enter name DatabaseFirstModel in the Name field.
Step 4 − Click Add button which will launch the Entity Data Model Wizard dialog.
Step 5 − Select EF Designer from database and click Next button.
Step 6 − Select the existing database and click Next.
Step 7 − Choose Entity Framework 6.x and click Next.
Step 8 − Select all the tables Views and stored procedure you want to include and click Finish.
You will see that Entity model and POCO classes are generated from the database.
Let us now retrieve all the students from the database by writing the following code in program.cs file.
using System; using System.Linq; namespace DatabaseFirstDemo { class Program { static void Main(string[] args) { using (var db = new UniContextEntities()) { var query = from b in db.Students orderby b.FirstMidName select b; Console.WriteLine("All All student in the database:"); foreach (var item in query) { Console.WriteLine(item.FirstMidName +" "+ item.LastName); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } } }
When the above program is executed, you will receive the following output −
All student in the database: Ali Khan Arturo finand Bill Gates Carson Alexander Gytis Barzdukas Laura Norman Meredith Alonso Nino Olivetto Peggy Justice Yan Li Press any key to exit...
When the above program is executed, you will see all the students’ name which were previously entered in the database.
We recommend you to execute the above example in a step-by-step manner for better understanding.