Building MVC Application via Code First Approach

|  Posted: August 10, 2022  |  Categories: .NET C# CSharp Git

Using Code First, we create POCO classes first, and then create a database from these classes. A useful feature of this code is when you don’t have a database available and you need to create one from scratch, then update it legitimately from your code if you don’t have a database prepared.

Using Entity Framework Core, this article will demonstrate how Code First can be applied to ASP.NET Core MVC applications. We reliably use EF to create, update, and match up the database with your model classes.

So what exactly is MVC?

An MVC (Model-View-Controller) is a software design pattern for implementing user interfaces, data, and controlling logic.

Then, What about Entity Framework?

An Entity Framework (EF) is an Object/Relational Mapping (O/RM) framework. Developers can access and store data in databases using an automated system based on ADO.NET.

When to Use the Code First Approach in MVC?

  • When the database is to be created.
  • When the application is to be made from scratch.
  • When the operations, such as creation and deletion of views, tables, and stored procedures.
  • When a database has many tables, stored procedures, and ideas.

Code First approach implementation: (make sure you have Dotnet SDK installed)

Blog management system using dotnet core mvc, which consists of Author, Post, and Category represents the models. Using EF-core, adding these models to SQL-Server and generate controller and views via aspnet-codegenerator.

  • Creating MVC project in VS Code
    • Run this command: dotnet new mvc -o blogMvcWebApp
  • Adding dependencies
    • dotnet add package Microsoft.EntityFrameworkCore.Design
    • dotnet add package Microsoft.EntityFrameworkCore.SqlServer
    • dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
  • Creating Models
    • Blog – Post (one-to-many)
    • Blog – Author (one-to-one)
    • Category – Post (many-to-many) 






  • Adding DB Context


  • Adding Connection Configuration

  • Applying Migration 
    • dotnet ef migrations add blog_db_init
    • dotnet ef database update

Now, our database will be successfully migrated and open SQL Server management studio to view our Database.

Blog Database

  • Scaffolding our model to controller and view, you can use this command in vs code terminal:
    • dotnet aspnet-codegenerator controller -name ControllerName -m ModelName -dc DBContextName -outDir Controllers


We have successfully created models for Blog management system via code-first approach using EF core and migrated to our SQL Server database.

Git is a free and open-source distributed version control system that can handle everything from tiny to extremely large projects quickly and efficiently. It was developed in 2005 by Linus Torvalds, creator of Linux Operating System.

To know more view about  GIT

Get notified about any future events

Interested in learning more about TechMeet360 or knowing about any future events? Sign up below to get notified.

Back to Top