Code First Approach in MVC: Everything You Need to Know

What’s Code First Strategy?

Code first strategy in MVC will get launched with Entity Framework 4.1. The code first strategy is used within the domain-driven design primarily. On this strategy, the applying area is contemplating. The lessons are created in line with the area entity as a substitute of the database. After that, the research have made that match the database design. The diagram under illustrates the code first strategy in MVC.

Code First Strategy in MVC

As clear from the determine, the entity framework creates or updates the database relying upon the area lessons. Therefore, the consumer must code first, after which the entity framework will create the database utilizing the code. That’s the reason it’s known as the code first strategy.

Code First Workflow

The code first strategy in MVC follows the workflow with the under steps:

  • Create the domain-driven lessons
  • Configure the area lessons created
  • Replace or create the database to the area lessons.

The configuration of area lessons happens utilizing the Fluent API, and the database replace is finished utilizing the automated migration.

When to Use the Code First Strategy in MVC?

  • When the database is to be created.
  • When the applying is to be constituted of scratch.
  • When the operations, similar to creation and deletion of views, tables, and saved procedures.
  • When a database has many tables, saved procedures, and concepts.

Earlier than, Introduce code first strategy in MVC, used DB First strategy generally. The DB first strategy has its benefits and might be most popular over the code first strategy within the under instances:

  • When the database is already created within the challenge.
  • When the challenge doesn’t contain many updates within the tables or views.
  • When the challenge is small.

Use Code First Strategy in MVC?

Earlier than utilizing the code first strategy, there are some steps to finish as given under:

1. Create a clean database

  • Open the SQL server and join with the database server.
  • In object explorer, right-click on the Database choice to create a brand new database.

2. Create MVC Mission

  • Navigate to File > New Mission > Visible C# > ASP .Internet Internet Utility
  • Enter a reputation for the answer and challenge and click on Okay.
  • Click on on MVC and alter the authentication choice to Particular person Consumer Accounts.

3. Create the Class Library Mission

  • Add a brand new challenge by right-clicking on the Answer Explorer.
  • Navigate to Visible C# > Home windows > Class Library and provides the challenge identify as ABC.DAL

4. Add Entity Framework to the DAL challenge created within the earlier step.

  • Navigate to DAL Mission > Handle NuGet Packages > abc.DAL.
  • Browse and set up the Entity framework.

5. Code First Strategy Implementation

Take into account the instance of an workplace having many staff working in a number of totally different departments. If the challenge entails creating an software for this workplace, any worker’s data might be considered and up to date. Utilizing the code first strategy, the lessons will get designed for the workplace area first. Take into account the 2 varieties of Worker and Division, the place every worker will get linked to 1 division.

Create the Worker class as under:

Public Class Worker {

Public int EmpId { get; set; }

Public string EmpName { get; set; }

Public float Age { get; set; }

Public DateTime DateOfJoining { get; set; }

Public float ExpInYears { get; set; }

Public Division Division { get; set; }


Create the Division class as under:

Public class Division {

Public int DeptId { get; set; }

Public string DeptName { get; set; }

Public ICollection<Worker> Worker { get; set; }


Code first makes use of the DbContext class to derive the context class. The context class exposes the DBSet, which is the gathering of entity lessons. The code for the creation of context class is as under:

Namespace EF6Console {

Public class OfficeContext: DbContext {

Public OfficeContext(): base()



Public DbSet<Worker> Worker { get; set; }

Public DbSet<Division> Division { get; Set; }



Now as context class is created, add worker utilizing it as under:

Namespace EF6Console {

Class ExProgram {

Static void major(string[], args) {

utilizing(var obj = newOfficeContext())


Var emp = new Worker() { EmployeeName = “Peter” };







Additionally Learn: Exception Dealing with Interview Questions

6. Reference DAL Mission to UI Mission

  • Add reference by right-clicking the References of UI Mission.

7. Allow Migration

  • Navigate to Instruments > Package deal Supervisor > Handle NuGet Packages for Answer and run the under instructions:


Add-migration Preliminary Create


8. Add Controller

  • Navigate to Controller > Add > New Controller and choose the MVC 5 Controller with views, utilizing Entity Framework.
  • Choose the mannequin class, context class, and format web page.


Utilizing the steps shared on this article, you should utilize the code-first strategy in MVC. Utilizing the code first strategy, you possibly can create the lessons and entities after which replace the database.

In the event you’re to study extra about full-stack improvement, take a look at upGrad & IIIT-B’s PG Diploma in Full-stack Software program Improvement which is designed for working professionals and affords 500+ hours of rigorous coaching, 9+ initiatives, and assignments, IIIT-B Alumni standing, sensible hands-on capstone initiatives & job help with high companies.

Turn out to be a Full Stack Developer



Download Server Watch Online Full HD

Socially Keeda

Socially Keeda, the pioneer of news sources in India operates under the philosophy of keeping its readers informed. tells the story of India and it offers fresh, compelling content that’s useful and informative for its readers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker