Book Image

ASP.NET Core 2 Fundamentals

By : Onur Gumus, Mugilan T. S. Ragupathi
Book Image

ASP.NET Core 2 Fundamentals

By: Onur Gumus, Mugilan T. S. Ragupathi

Overview of this book

The book sets the stage with an introduction to web applications and helps you build an understanding of the tried-and-true MVC architecture. You learn all about views, from what is the Razor view engine to tagging helpers. You gain insight into what models are, how to bind them, and how to migrate database using the correct model. As you get comfortable with the world of ASP.NET, you learn about validation and routing. You also learn the advanced concepts, such as designing Rest Buy (a RESTful shopping cart application), creating entities for it, and creating EF context and migrations. By the time you are done reading the book, you will be able to optimally use ASP.NET to develop, unit test, and deploy applications like a pro.
Table of Contents (14 chapters)

Create EF Context and Migrations

For starters, we need to install Entity Framework and its tools to our Infrastructure project.

Follow these steps to create the EF context:

  1. Just right-click on dependencies and select Manage NuGet Packages.
  2. Afterwards write Microsoft.EntityFramework.Core.SqlServer in the search box and install it.

Your screen should look as follows:

  1. Similarly, install the Microsot.EntityFrameworkCore.Tools package, as follows:

So, your project folder for Infrastructure looks as follows:

  1. Next, we create the EF folder in the Infrastructure project and implement our DbContext with a class called RestBuyContext. Make sure you have a reference to the RestBuy project from Infrastructure. Use this code:


Go to to access the code.

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using RestBuy.Entities;
using System;
using System.Collections.Generic;
using System.Text;
void ConfigureStockAmount(EntityTypeBuilder<StockAmount...