In this article you will get answers to the following questions.
- What is Database First?
- How do we implement Entity Framework Database First in WebForm?
Entity Framework is ORM (Object Relation Mapping) tool which is used to get connected with the database. Entity Framework is a part of ADO.NET under .NET Framework. There are three approaches to get connected with database.
- Database First
- Model First
- Code First
What is Database First?
In this method, model or entity is generated as per database. First, we have to design the database and then, the rest of the things are created. When we go for a Database First approach, we’re fully dependent on what gets generated for our models in our application. Occasionally, the naming convention is undesirable and not as we want. Sometimes, the relationships and associations aren't quite what we want. Code First is better than Database First in this way.
This method is good where project gets developed on the basis of database and the existing application is developed in Windows Form.
My database name is MbkTest. It has one table called tblFriends.
tblFriends table structure
- USE [MBKTest] GO
- /****** Object: Table [dbo].[tblFriends] Script Date: 10-Jan-17 5:49:25 PM ******/
- SET
- ANSI_NULLS ON GO
- SET
- QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblFriends](
- [FriendID] [int] IDENTITY(1, 1) NOT NULL,
- [FriendName] [nvarchar](50) NULL,
- [FriendPlace] [nvarchar](50) NULL,
- [FriendMobile] [nvarchar](10) NULL,
- [FriendEmailID] [nvarchar](200) NULL,
- CONSTRAINT [PK_tblFriends] PRIMARY KEY CLUSTERED ([FriendID] ASC) WITH (
- PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
- IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
- ALLOW_PAGE_LOCKS = ON
- ) ON [PRIMARY]
- ) ON [PRIMARY] GO
Sample records of table tblFriends.

How to implement Entity Framework Database First in Web Form

Right click on project and select Add >> Add New Item >> WebForm.

Add a new Web Form named “Default.aspx”.

To implement the database first approach, we should have ADO.NET Entity Data Model.
Right click on the project name and select Add-->Add New Item >> ADO.NET Entity Data Model.

Add a new ADO.NET Entity Data Model named “MbkTestModel.edmx”.

As you click on "Add" button, the following dialog box will appear on the screen. This dialog box is asking you to put ADO.NET Entity data model in APP_CODE folder. Please select "Yes".

As you click on Yes button, the Entity Data Model Wizard dialog box will appear on the screen, asking you to select any one out of two options.
Following are the two options.
- Generate from database To generate a model from existing database.
- Empty Model To generate a database from model.
We will select the first option i.e. "Generate from database". Because we are using Database First, so ADO.NET Entity data model gets generated from the existing database.

As you click on "Next" button, the following screen will appear.












ismail alabouPosted Sep 25, 2022, 5:12 PM
Where can i find sql file to generate db ??
Vishal PrajapatiPosted Jan 13, 2017, 12:20 AM
Nice explanation..Easy to understand