EF Database First with ASP.NET MVC: Creating the Web Application and Data Models

By Tom FitzMacken|

Using MVC, Entity Framework, and ASP.NET Scaffolding, you can create a web application that provides an interface to an existing database. This tutorial series shows you how to automatically generate code that enables users to display, edit, create, and delete data that resides in a database table. The generated code corresponds to the columns in the database table.

This part of the series focuses on creating the web application, and generating the data models based on your database tables.

Create a new ASP.NET Web Application

Create a new project in Visual Studio, and select the ASP.NET Web Application template. Name the project ContosoSite.

create project

Click OK.

In the New ASP.NET Project window, select the MVC template. Click OK to create the application.

select mvc template

The project is created with the default files and folders.

In this tutorial, you will use Entity Framework 6. You can double-check the version of Entity Framework in your project through the Manage NuGet Packages window. If necessary, update your version of Entity Framework.

show version

Generate the models

You will now create Entity Framework models from the database tables. These models are classes that you will use to work with the data. Each model mirrors a table in the database and contains properties that correspond to the columns in the table.

Right-click the Models folder, and select Add and New Item.

add new item

In the Add New Item window, select Data in the left pane and ADO.NET Entity Data Model from the options in the center pane. Name the new model file ContosoModel.

create model

Click Add.

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

generate from database

Click Next.

Select the data connection you added earlier.

connect to database

Click Next.

Select Tables to generate models for all three tables.

select tables

Click Finish.

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

The models are generated from the database tables, and a diagram is displayed that shows the properties and relationships between the tables.

diagram of model

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

show new model files

The ContosoModel.Context.cs file contains a class that derives from the DbContext class, and provides a property for each model class that corresponds to a database table. The Course.cs, Enrollment.cs, and Student.cs files contain the model classes that represent the databases tables. You will use both the context class and the model classes when working with scaffolding.

Before proceeding with this tutorial, build the project. In the next section, you will generate code based on the data models, but that section will not work if the project has not been built.

This article was originally created on October 30, 2013

Author Information

Tom FitzMacken

Tom FitzMacken – Tom FitzMacken is a Senior Programming Writer on the Web Platform & Tools Content team.