Quantcast
Viewing latest article 5
Browse Latest Browse All 15

ASP.NET MVC Tutorial with EF6 Model First Approach

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.Image may be NSFW.
Clik here to view.
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”.Image may be NSFW.
Clik here to view.
Entity Framework Tutorial

Choose Empty EF Designer model from Entity Data Model Wizard popup. Image may be NSFW.
Clik here to view.
Entity Framework Model First

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

Image may be NSFW.
Clik here to view.
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”.Image may be NSFW.
Clik here to view.
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:Image may be NSFW.
Clik here to view.
EF 6 Navigation Properties

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

Image may be NSFW.
Clik here to view.
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. :
Image may be NSFW.
Clik here to view.
EF 6 Category Entity

Similarly, we can do this for Product entity.

Image may be NSFW.
Clik here to view.
EF 6 Product Entity

Add association of Product with Category entity. It is done as :Image may be NSFW.
Clik here to view.
EF 6 Association

Image may be NSFW.
Clik here to view.
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.Image may be NSFW.
Clik here to view.
Entity Framework Entities

Configuring Model Properties

Just click on the “Entity Data Model Designer” surface and open the properties window: Image may be NSFW.
Clik here to view.
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:Image may be NSFW.
Clik here to view.
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.

Image may be NSFW.
Clik here to view.
EF 6 Database from Model

Put valid database details and click on “Test Connection”.Image may be NSFW.
Clik here to view.
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.Image may be NSFW.
Clik here to view.
EF Designer DDL Script

This query generate the database for our app. It can be executed by the options as shown in below screen:Image may be NSFW.
Clik here to view.
Execute Script

We can check our database and will find following schema:Image may be NSFW.
Clik here to view.
EF 6 Model First Approach

Now create “CategoriesController” as shown in below image:Image may be NSFW.
Clik here to view.
ASP.NET MVC Controller

Image may be NSFW.
Clik here to view.
ASP.NET MVC Add Controller

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

Image may be NSFW.
Clik here to view.
ASP.NET MVC Tutorial

Create some categories, the category listing will appear as below:Image may be NSFW.
Clik here to view.
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.

Similarly we can add product controller.Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
ASP.NET MVC Tutorial Controller

Click on create new link, following screen will appear:Image may be NSFW.
Clik here to view.
ASP.NET MVC New Product

We can find all categories will appear in category drop down.Image may be NSFW.
Clik here to view.
ASP.NET MVC New Product

After creation of products, we get the list of product as follows:Image may be NSFW.
Clik here to view.
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

The post ASP.NET MVC Tutorial with EF6 Model First Approach appeared first on Web Development Tutorial.


Viewing latest article 5
Browse Latest Browse All 15

Trending Articles