Entity Framework DB First Approach

DB First approach let us create an app from an existing database. Let's learn how DB First approch is used to build ASP.NET MVC app.

Firstly, we need to create a database.

Open the MS SQL Server Management Studio and create a database; you can use SQL Script to create a database.

Step 1: Creating a Database.

  • Right click on to Databases folder and click New Database. Here's the figure:

    Creating a Database

  • Here I created a database with name “MyDB”. After that click the OK button. You can create a database of whatever name.

You can create a database with the help of queries.

Click New Database

After clicking we can see the following window:

  • Select the database, and right click on to the Tables, New, then click Table, to create a table.

    Create a Database

    In the Table as in the following figure, I have five fields of the “UserInfo” table:

    • Id
    • FName
    • LName
    • Address
    • Email

      Table

  • The Id Column is marked with the Primary key.

    Primary key

  • After creating this table save table name (whatever your wish). I have given the table name “UserInfo.” Click on OK button.

    UserInfo

    Now our Database is ready.

Step 2: Open Visual Studio and create a new project in Visual Studio and select the ASP.NET Web Application. Click on to the File, New, then Project, to add the project.

New Project

Select the Templates, then Visual C# and click ASP.NET Web Application, Name the project whatever you like. In this I have given it the name “MVCDemoTest”.

Click on to the OK button. For clarification see the following image:

ASP.NET Web Application

After clicking the OK button, you see the following figure.

Select the MVC, because I will work in MVC application. Click on to the OK button.

MVC

You will now create Entity Framework models from the database tables.

Right click on to your project to add a new item:

add a New item

Select Data in the left pane and ADO.NET Entity Data Model from the options in the center pane. Name the new model file “UserInfoModel”.

After this click the “Add” button.

UserInfoModel

In the Entity Data Model Wizard, select EF Designer from database.

Click on to the “Next” button.

EF Designer from database

Click the New Connection button to add a database connection defined with your environment.

New Connection button

After clicking on  New Connection button, you will see the following figure of Connection properties:

Choose the server name.

Choose your server name

After providing the server name, select the database name which you have created, from the available databases.

Here I have selected “MyDB”, because this is the database of my project.

MyDB

If we want to check our connection, click on to the Test Connection button. If Test Connection succeeded, click OK button.

connection

The connection properties are now displayed.

We can change the default name for connection in the Web.Config file, here I have given “UserInfoContext”.

And include sensitive data to the connection string.

After this click on to the “Next” button.

Click on to the Next

Select Tables to generate models.

I have given the name of Model Namespace as “UserInfoModel”.

Click on to the “Finish” button.

Note: If you receive a security warning, select OK to continue running the current template.

continue running the current template

The models are generated from the database tables, and a diagram is displayed that shows the properties of particular table.

The Models folder now includes many new files related to the models that were generated from the database.

Now you can see our model is created by entity framework.

properties

Now our Database and Model are ready.

Next we have to create a controller;  let’s create a controller.

Right click on the Controllers and Add a Controller.

add new controller

In this case I am going to use MVC 5 Controller - Empty.

Click on the “Add” button.

MVC 5 Controller-Empty

After clicking on to the “Add button,”you can see the following figure:

Here in this you can give the Controller name, whatever you like, I have given “UserInfoController”.

Click on to the “Add” button.

Add Controller

You can see the following “UserInfoController” which is inherited by the Controller:

Controller

UserInfoController

We need to add an Index view.

Right click on to the Project coding and click on “Add View”.

add another view

You can see, the default view is Index.

In the template, you can select “List”.

List

Model class is our Project Name, you can select it.

Data Context Class is UserInfoContext, select it.

Click on to the “Add” button.

Data Context Class

In the following figure, this is our Index View.

code

Now we can debug our project. Select "Start Without Debugging" from the DEBUG menu.

debug project

In the following figure this is our Index view:

Index view

In the following figure this is our Index view.

If you want to click on “Create New” this will give the server error.

Because here I need to add a new view to adding new records into database using EF.

new records into database

Now we are going to add a “Create View”.

Right click on to the project code and click on to the “Add View” (In the red Circle).

Add view

Here in the figure there is a default view “Create.

In the Template, select “Create”.

select the Create

The Model Class and Data context class will be the same as above method.

Click on to the “Add” button.

Click on to the Add

In the following figure “Create.cshtml” is the Create view (In the red circle).

Create.cshtml

Now we can debug our project:

debug our project

Before running the project, we have to apply one more action that is SaveChanges();, without this our database won't work.

Changes in the “UserInfoController.cs” class.

UserInfoController.cs

Now we can run the project:

run the project

Click on to the “Create New” to add the field in the database.

Click on to the Create New

After filling the details, click on to the “Create” button.

Create

Now it is created; we can create more records by simply clicking on the “Create New” button.

Create New

Now we can check into our database:

Check into our Database

This was my article on ASP.NET MVC 5.0 using Entity Framework Database First Approach with scaffolding templates.

Thanks for reading this article and you can download my new book on “.NET Interview Questions and Answers” with practical implementations and examples.

Read more articles on ASP.NET:

 

Author
Nitin Pandit
12 52.9k 31.4m
Next » Entity Framework Code First Approach