ASP.NET MVC Tutorial with EF6 Model First Approach

By | December 20, 2016

In this ASP.NET MVC Tutorial, we are going to create a basic ASP.NET MVC application with Entity Framework 6 using Model First Approach. In our previous Tutorials on ASP.NET MVC, we created applications using EF 6 with Code First Approrach as well as Database First Approach. Also, we discussed all Entity Framework related concepts in detail in another Entity Framework Tutorial. We can follow below links to both of these Entity Framework Tutorials:

In the Model First approach, we create entities, relationships, and inheritance hierarchies directly on the design surface of EDMX and then generate database from our model.EF 6 Model First Development

So, Let’s start by creating a new ASP.NET MVC Application and adding new ADO.NET Entity Data Model as below. Right click on our DAL(Data Access Layer) folder, choose “Add” and then “ADO.NET Entity Data Model”.Entity Framework Tutorial

Choose Empty EF Designer model from Entity Data Model Wizard popup. Entity Framework Model First

Once we click on finish, we will get below screen:

Entity Framework Designer Model

We can create entities, association and inheritance between them on an empty designer by right clicking on designer surface. Choose “Add New” and then “Entity”.Entity Framework 6 Entities

EF 6 Navigation Property

Navigation property in Entity Framework 6 is a used to represent Foreign Key relationships between the entities just like Foreign Key relationship in database.  Add Entity details as given in below screenshot, we are creating an Entity with name Category:EF 6 Navigation Properties

Further, we can add other properties of category as below:

Entity Framework Scalar Properties

Finally the category entity will look as below with Properties as CategoryId and CategoryName. For Navigation Properties i.e. used for Foreign Key relationship, we will see after creating all related entities. :
EF 6 Category Entity

Similarly, we can do this for Product entity.

EF 6 Product Entity

Add association of Product with Category entity. It is done as :EF 6 Association

EF 6 Add Association

 

So, a One-to-Many association is developed between Category to Product. A single Category can have many Products associated, For example, Electronics Category will contain many electronic products like Mobile, Camera etc.Entity Framework Entities

Configuring Model Properties

Just click on the “Entity Data Model Designer” surface and open the properties window: Configuring Model Properties

Here we can modify the marked properties if we wish or keep the default.

Now, we need to generate database from model. Do it as:Generate Database from Model

Giving the Connection Properties of our database. Being a basic example, we are using Windows Authentication but for a professional project, we will provide proper properties and related authentication mode.

EF 6 Database from Model

Put valid database details and click on “Test Connection”.Test Connection

If this message appears, means your server is able to recognize and connect the database, otherwise there is any issue in the data we entered.

After completing all steps, we will get following screen.EF Designer DDL Script

This query generate the database for our app. It can be executed by the options as shown in below screen:Execute Script

We can check our database and will find following schema:EF 6 Model First Approach

Now create “CategoriesController” as shown in below image:ASP.NET MVC Controller

ASP.NET MVC Add Controller

Run the application and access “/category”, we will get following screen:

ASP.NET MVC Tutorial

Create some categories, the category listing will appear as below:ASP.NET MVC Category Tutorial

  • Learn ASP NET MVC 5 step by step [Maruti Makwana, Corporate Trainer] 28 Lectures, 2.5 Hours Video, Intermediate Level
    Very easy to learn video series on Asp.Net MVC 5 Specially for those who are familiar with Asp.Net Web forms.
  • AngularJS for ASP.NET MVC Developers [Brett Romero] 10 Lectures, 1 hour video, Intermediate Level
    The Fastest Way For .NET Developers To Add AngularJS To Their Resume
  • ASP.NET with Entity Framework from Scratch
    [Manzoor Ahmad, MCPD | MCT] 77 Lectures, 10 hour video, All Level
    Latest approach of web application development
  • Comprehensive ASP.NET MVC [3D BUZZ] 34 lectures, 14 Hours Video, All Levels
    From zero knowledge of ASP.NET to deploying a complete project to production.

Take this Course Now on Discounted Price

Similarly we can add product controller.ASP.NET MVC Product Controller

It will automatically generate the code for CRUD operations through scaffolding technique.

When we access “/Products”, following screen will appear:

ASP.NET MVC Tutorial Controller

Click on create new link, following screen will appear:ASP.NET MVC New Product

We can find all categories will appear in category drop down.ASP.NET MVC New Product

After creation of products, we get the list of product as follows:ASP.NET MVC Product Listing
Hopefully, this basic example for ASP.NET MVC Application with Model First Approach using two related entities i.e. Category & Product will be helpful in practically understanding the concept.


You have a table with one column and Unique constraint set. You have set EnforceConstraints to True and populated the DataSet with the table. What would happen if you are creating new rows with identical values?[Choose One]:

  • A. System Error
  • B. It accepts new values
  • C. Compiler error
  • D. Throws ConstraintException

To further test your ASP.NET MVC/EF skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.

Further If you are going to appear for a Technical Interview on EF, you can get below a comprehensive list of Entity Framework Interview Questions and Detailed answers for beginners as well as advanced concepts.

 Correct Answer: D