ASP.NET MVC Application with EF6 Code First Approach

By | November 30, 2016

In this ASP.NET MVC Tutorial, we are going to see a simple but practical implementation for Entity Framework Code First Approach. We have already given examples on Entity Framework Model First as well as traditional Database First approaches in other examples. You can follow other sample implementations at below given URLs.

While using Code-First Approach, we start with creating classes in code first as per our domain requirements instead of doing database design(the traditional way). On the basis of our classes and provided configuration, Code-First APIs will create the database on the fly.Entity Framework Tutorial

So, for the sake of this implementation, we will start by writing C# code (creating C# classes for domain as well as context classes). When the application will run, Code First APIs will create the new database (if it does not exist yet) and map classes with the database using default code-first conventions.
Career skills to jumpstart your future.

Entity Framework Code First Tutorial

Following are the steps we can follow for Entity Framework Code First Approach:

Create visual studio project “Entity_Framework_FAQ”.ASP.NET Web Application

On next screen choose template as MVC.ASP.NET MVC Project Template

Click “OK”, Your application will be created and you get following welcome screen:Developing ASP.NET MVC Application

If you have not installed Entity Framework, please install it by using Nuget Package Console Command as follows:

Or follow the link to Install Entity Framework in Visual Studio using step by step approach.

In Code-First approach, Instead of designing database tables first, classes are created first.

  • 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.
In our project, we will create two classes, Category class and Product class.

Create class “Category.cs” as shown below:EF Code First

EF Code First Approach

Add following code in “Category.cs” as shown below:

Similarly add product class and add following code to it:

Add a data context class that will contain the category model and product model:

It will contain following code:

Add new Controller “Products” as shown in below steps:ASP.NET MVC Controller

MVC5 Controller with Entity Framework

After click on “Add” button, the scaffolding technique will put all the necessary code in the respective controller and related views.

Product Controller will contain following code:

Similarly add “Categories” controller. Categories controller will contain following code:

Put connection string in web.config as:

When you run the application and access “/categories”, it will automatically create the database same as the name of Context class i.e.MS EF Code First Approach

While accessing the url ”Categories”, following screen will appear:All Categories in MVC

Click on create new, to create new category.

EF New Category

After creation of categories, let’s create products.

Access “/Products”, following screen will appear,EF Product List

Create new product,EF New Product

This is very basic example for ASP.NET MVC Application with Code-First Approach using two related entities i.e. Category & Product. Hopefully, it will be helpful for understanding the approach in a more practical way.

Coursera - Hundreds of Specializations and courses in business, computer science, data science, and more

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