Restoring a SQL Server database backup file (.bak)

Introduction On regular basis we need to restore the database, either will only schema of the database, or many times both data as well as schema.  Most of the applications (either Web application e.g. ASP.NET based apps, or Windows applications e.g. WPF based apps) requires 'reference data' such as drop-down data, types of users, etc. So what is the most optimum, reliable way of moving database either on separate database server, or on the same database server but with different database name? There are several ways to achieve this objective but my personal preference is to first create the database backup file i.e. 'my_backup.bak' using Microsoft SQL Server Management Studio , and then restoring that backup file to create new database.  This article focuses on restoring the backup file to create a new database. How to create the backup file will be covered in another article.   This article addresses below points: 1.   How to check the name and path of the files

Basics of core web app (MVC) project structure


We have tried to cover the basics from implementation perspective, i.e. when you create a new core project in Visual Studio, it is essential to know the overall structure generated by the Visual Studio.

Fig-1: Visual Studio 2019

Project Structure

Below are the important files that developers must be familiar with, though we are covering MVC here but this information is useful even for non-MVC core web applications. 

Program.cs File

The Program class mainly handles everything associated with the hosting environment. The Visual Studio will generate below mentioned two static methods:

1. CreateHostBuilder Function

This static method creates and returns the HostBuilder object (the details about HostBuilder is discussed in below point). 

2. Main Function

The main function is the entry-point for the application. It is calling the CreateHostBuilder function, and then calling Build and Run functions on the returned HostBuilder object. 

Calling Build (which can be run only once) on HostBuilder object returns the Host object, which is an encapsulation of IHostedService interface implementation, Dependency Injection (DI), Logging, and Configuration. These are separate topics, which need blog of their own, so for now you just need to understand that the return Host object encapsulates these four main resources. 

Finally the Run function, as its name suggests runs the application and block the calling thread until host shutdown, in simple words it keeps running the main application thread till the application is running. 

Startup.cs File

The Startup class is all about the preloading and configuration of your services and middleware. Below are the two main methods that we need to know. 
  1. The ConfigureServices method, called by the runtime and used to add services to the container
  2. The Configure method used to configure the HTTP pipeline

Basic Project Structure

For a Core MVC web applications, below is the typical folder structure, and the generated files:
  • A Controllers folder contains all of the controllers of your application. Below section elaborates on Controllers.
  • A Services folder contains all the services of your application (for example, external communication services).
  • A Views folder contains all the views of your application. This folder should contain a single Shared subfolder as well as one folder per controller. Refer below sections for details. 
  • A _ViewImports.cshtml file defines the namespaces, which you want to be available in all views.
  • A _ViewStart.cshtml file defines the code, to be executed at the start of each view rendering 
    • e.g., setting the layout page for all the views.
  • A _Layout.cshtml file defines a common layout for all of your views.

Models, Views, and Controllers

Model-View-Controller design pattern needs its own article, so those we will cover in another dedicated blog for MVC, and here we will just briefly describe these folders and their expected contents.

Before creating Controller make sure your model is already there.
  • You will find the Models folder at the main solution-level, create it if does not exists.
  • Create the Class (e.g. EmployeeViewModel) for all you models, which will have the needed properties (e.g. Name, Department, etc.) to hold the data.
  • As a naming convention make sure your Model name ends with ViewModel.
Now right-click on the Controllers folder, which will also be at the main solution-level (create if does not exists), and choose Add >> Controller (choose - "MVC Controller - Empty")
  • By default it will create a single method called - Index 
  • As a naming convention make sure your Controller name ends with Controller.

Fig-2: Add Controller

Next, right-click on this Index method, which is in the newly created Controller, and select "Add View...", then choose "Razer View - Empty", or "Razer View".
  • This will generated automatically the Index.cshtml file after created a new folder with the name of the controller (this in case Employee folder), and it will add that cshtml file into this folder. 

Fig-3: Add View


We have discussed the typical structure of the core web application, which was based on Model-View-Controller framework. The discussion was specifically around Visual Studio generated code. 

Related Topics

Dependency Injection


Your feedback

Your feedback is of paramount importance to us, provide your valuable inputs; what else you would like to read about, or if you would like the author to elaborate on any specific topic from this article, or any other article on this blog.


The material presented in this blog is to help educate the developers, and the author(s) & owner(s) of this blog are not responsible of any damage arising out of use of the code snippets or any other contents of this blog.