Tech Junkie Blog - Real World Tutorials, Happy Coding!: Hour 13 ASP.NET Core : Add NorthwindContext To Startup Class And Create Database

Thursday, August 22, 2019

Hour 13 ASP.NET Core : Add NorthwindContext To Startup Class And Create Database

In this post will are going to finally create the database that we have been preparing for in the last previous blog posts.  It's a two step process, first you have to add the NorthwindContext to the application in the Startup class, then you have to run the Entity Framework migration tool.

Here are the steps to create your NorthwindCafe database:

1.  Open the Startup.cs file, then type the following lines in the ConfigureServices method
            var connectionString = Configuration["Data:NorthwindContextConnection"];

            services.AddDbContext<NorthwindContext>(options => options.UseSqlServer(connectionString));

The line above gets the connection string from the appSettings.json file that we've created earlier. Then use the AddDbContext method in the services instance.  Dependency injection will take care of the plumbing for you.  Using lamba expression we tell the Entity Framework to use the Sql Sever provider for Entity Framework core.

Make sure you have the following namespaces in your Startup class

using NorthwindCafe.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;

2.  Now we can run the dotnet ef tools in the command line, make sure you are at the location of the NorthwindCafe.Web project.  Also before you run the tool in command line, make sure you build the solution first.

3.  In the command line type dotnet ef migrations add InitialDatabase your screen should look like below

4.  Now you have to run another command to actually create the database  type dotnet ef database update to create the NorthwindCafe database in SQL Server

The first command dotnet ef migrations add InitialDatabase creates two files in the "Migrations" folder in the NorthwindCafe.Web project.  These files contains the codes for creating the database and any other subsequent migrations that you will run in the future.  Everytime you run a migration a snapshot is created with the NorthwindContextModelSnapshot

The second dotnet ef database update command uses the migration script in Visual Studio to update the database.  After you run this command you will see that the NorthwindCafe database has been created.  The amazing thing is that the foreign key constraint has been added for us, because we used the virtual keyword to define our property in our models in this post.


  1. Home furniture's and wood working makes home more beautiful and attractive. its Christmas aslo i think this site is of great help for us Teds Woodworking

  2. Amazing post and also great suggestions! This publication is very beneficial and great for us. Thanks for discussing valuable details. coronavirus

  3. It's really nice and meaningful. it's really cool blog. Linking is very useful thing. you have really helped lots of people who visit blog and provide them useful information

  4. Thanks for sharing the article. This is really informative. 2nd Corona wave

  5. Helpful information for those who knew nothing about this �� thanks for pulling together and sharing! National Anti-Money

  6. Great job for publishing such a beneficial web site. I was exactly searching for. Thanks for such a post and please keep it up. I am very happy to see your post. Thanks for sharing.
    South africa's News

  7. It was really good work. I really like your thoughts.I personally appreciates for your work.your article is very impressive and helpful for us. Hope you will share these kinds of things with us also in future.
    Apple music launch 1st radio show

  8. Recently, I have started to read a lot of unique articles on different sites, and I am enjoying that a lot. Although, I must tell you that I still like the articles here a lot. They are also unique in their own way. COFI BILL

  9. Thanks for giving us so much shine !!
    Great tips man! Worth reading. To be honest at first sight I thought you’ll use the same old tips people used to write on their blogs, but here I learnt some new things in this key Point, something out of the box!
    OPPO Launches F17 Pro

  10. Your blog is very effective and valuable. I am very glad to read your blog. I hope you will soon share your next post about this discussion. Healthcare network

  11. We will finally create the database that we have been planning for in the previous blog entries in coursework help ukk this post. It's a two-step process: first, add the NorthwindContext to the Startup class of the application, then run the Entity Framework migration tool.


Search This Blog