Entity Framework – New Database First Tutorial With ASP.NET 5 For Beginners
27 February 2023

Entity Framework – New Database First Tutorial With ASP.NET 5 For Beginners

By

In this chapter, we will learn Entity Framework 6 with a new database with the help of easy programming example. We will create a demo Entity Framework Project here and explain all the steps briefly so you can understand it clearly. Before starting this chapter, following knowledge is required.

1. Basic knowledge of ASP.Net MVC
2. SQL Server 2005/2008/2012
3. C# Programming Language

I. ABOUT THIS CHAPTER

Here, I am going to create a simple MVC 5 project with the help of Entity Framework 6. This project will do the following thing.

1. One Page Design
2. Create Database and Table
3. Accept Entries from the User.
4. Save them to database
5. Display Data
6. Edit Data
7. Update Data
8. Delete Data

After finishing project, your application will look like:

EF - Database First Output

II. Start A New ASP.NET Web Application (.NET Framework) Project

Here, I am using Visual Studio 2015 but you can use any of VS 2010/2012/2015 or 2017 version. You just have to create any version of MVC Project.

There are 5 parts in this project and this is 2nd Part.

PART 2:

PART 1. EF – New Database First Overview
PART 2.Start a New ASP.NET MVC Project
PART 3. Create New Database and Table for EF
PART 4. Install Entity Framework and Create Entity Data Model
PART 5. CRUD Operation using Entity Framework 6

STEP 1:

Open Visual Studio 2015 and Start a New Project.

EF New Project

STEP 2:

Select Web in the left panel and then select ASP.NET Web Application (.NET Framework) from middle panel. Give Project Name FirstEFProject and click OK.

EF New Project

STEP 3:

Select MVC in template window and change authentication to No Authentication and click OK.

EF New Project

III. Create New Database And Table For Entity Framework

This project is about storing and retrieving Book Details in the library so we will create database and table according to the project.

There are 5 parts in this project and this is 3rd Part.

PART 3:

PART 1. EF – New Database First Overview
PART 2. Start a New ASP.NET MVC Project
PART 3.Create New Database and Table for EF
PART 4. Install Entity Framework and Create Entity Data Model
PART 5. CRUD Operation using Entity Framework 6

STEP 1:

Create Database LibraryDBOpen Server Explorer in Visual Studio. Go to View > Server Explorer.

Create Database for EF

STEP 2:

Right Click on Data Connections and click on Create New SQL Server Database.

Create Database for EF

STEP 3:

Give Server Name as follows and Create Database LibraryDB.

STEP 4:

Add a New Table BookDetails in the database. Expand LibraryDB in server explorer and right click on Tables and click on Add New Tables.

Create Database for EF

STEP 5:

Create a table as mentioned in picture. After designing table click on Update button to save it into the database.

Create Database for EF

STEP 6: SQL SCRIPT

CREATE TABLE [dbo].[BookDetails]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [BookName] NVARCHAR(50) NULL, 
    [Price] NVARCHAR(50) NULL, 
    [Category] NVARCHAR(50) NULL, 
    [AuthorName] NVARCHAR(50) NULL, 
    [Edition] NVARCHAR(50) NULL, 
    [BookCondition] NVARCHAR(50) NULL, 
    [Available] NCHAR(10) NULL
)

SUMMARY:

In this chapter, you created SQL Database and a Table. In the next chapter, we will Install Entity Framework and Create Data Model.

IV. Install Entity Framework And Create Entity Data Model

There are 5 parts in this project and this is 4th Part.

PART 4:

PART 1. EF – New Database First Overview
PART 2. Start a New ASP.NET MVC Project
PART 3. Create New Database and Table for EF
PART 4.Install Entity Framework and Create Entity Data Model
PART 5. CRUD Operation using Entity Framework 6

If you haven’t install Entity Framework yet, then you can install easily. Just read the following installing guide.

1. Right click on Project Name in Solution Explorer and Click Manage NuGet Packages…
2. NuGet Package Manager Window will be open. Here, Browse for Entity Framework. Now, Click Install Icon or Install button to Installing it into your project.
3. A Review Changes Dialog box will appear. Click OK.
4. Accept License Agreement by clicking on I Accept button.
5. Now, you can see Entity Framework under References in Solution Explorer.

Here, is a Step by Step Pictorial Guide of Installing Entity Framework 6. Install Entity Framework 6 Pictorial Guide

CREATE ENTITY DATA MODEL

STEP 1:

Right Click on Model Folder in Solution Explorer and click Add > New Item.

Create Entity Data Model

STEP 2:

Select Data in Left Pane and then choose ADO.NET Entity Data Model in the middle pane. Give the name to BookDataModel and click Add.

Create Entity Data Model

STEP 3:

Entity Data Model Wizard will appear that will ask you to choose your data model. Choose EF Designer from Database and click Next.

Create Entity Data Model

STEP 4:

In the next wizard choose the connection properties for your connection string. Just check the radio button saying “Yes, include the sensitive data in the connection string.” And make sure the check box is checked which asked you to “Save connection setting in Web.Config as:” and click Next.

Create Entity Data Model

STEP 4:

Select Entity Framework 6.x if Wizard asks you to choose Entity Framework version and click Next.

Create Entity Data Model

STEP 5:

In the next Windows select object of the database. You must select your table BookDetails and click Finish.

Create Entity Data Model

STEP 6:

If everything goes fine then you will see your BookDataModel in designer mode.

Create Entity Data Model

Congratulations! You have successfully created entity data model from new database. Now you will see that inserting, updating and deleting operation with a table is so much easy with the help of your BookDataModel.

V. MVC 5 CRUD Operation Using Entity Framework 6

Till now, you have completed all the previous chapters. This is the time to combine all the work together and display output in a web browser. You are using Entity Framework so, you don’t need to worry about writing SQL queries and connection for database operation. All database part will be handled by EF automatically.

There are 5 parts in this project and this is 5th Part.

PART 5:

PART 1. EF – New Database First Overview
PART 2. Start a New ASP.NET MVC Project
PART 3. Create New Database and Table for EF
PART 4. Install Entity Framework and Create Entity Data Model
PART 5.CRUD Operation using Entity Framework 6

Add a new Scaffolding Item to Controller

STEP 1:

Right click on Controller folder. Select Add > New Scaffolded Item.

MVC 5 with Entity Framework 6

STEP 2:

A window will open. Select MVC5 Controller with views, using Entity Framework and click on Add button.

MVC 5 with Entity Framework 6

STEP 3:

An Add Controller window will open. In Model class, select BookDetails (FirstEFProject.Models) and in the Data context class, select LibraryDBEntities (FirstEFProject.Models) and click on ADD button.

MVC 5 with Entity Framework 6

STEP 4:

Now, see the Solution Explorer. You will notice that a BookDetailsController.cs is automatically added. You will also notice that BookDetails Views is also automatically added.

MVC 5 with Entity Framework 6

STEP 5:

Set BookDetailsController to start page. Expand App_Start folder in solution explorer and open RouteConfig.cs.

App_Start Folder

STEP 6:

Here change controller name Home to BookDetails

defaults: new { controller = "BookDetails", action = "Index", id = UrlParameter.Optional }

STEP 7:

Press F5 to run your project.

EF - Database First Output

OUTPUT PICTURE

EF Output 1
EF Output 2
EF Output 3
EF Output 4
EF Output 5
EF Output 6

SUMMARY:

Till now, you learned how to create an MVC 5 Project with Entity Framework 6 with Database First. I hope you won’t get any trouble creating this project. If you have any trouble then you can post your problem in the comment section. In the next chapter.

Prev Post

CRUD Operations using Entity Framework…

Next Post

How to install WampServer in…

post-bars