Skip to main content

Back-End Development

Adding a Database Project to a .NET MVC Framework Web Application

Man Working At His Computer

For an updated tutorial with GIFs, click here.

We are going to be continuing from where we left off in the last post. If you are trying to follow along and want to take a look at how I set up the folder structure, click here.

Overview

  1. Create a SQL server database project and configure the path to the ’04 – Database’ folder (or whichever folder you want to place it in).
  2. Add an existing project in the Visual Studio.
  3. Add scripts and table files.

Details

  1. Create a new SQL Server Database Project. This is the database where the schema and the scripts will go.

B2 P8

  1. Name your project and configure the path to the ’04 – Database’ folder we created earlier. I will name it ‘Demo_01.Database’ in this example.

B2 P9

Demo_01.Database project will open up but simply close it and go back to the original Demo_01.sln file. However, you will notice that the .Database project you just created is not visible in the solution.

B2 P10

  1. Right click the solution and Add an Existing Project. Locate the SQL server database project you created in Step 1 and click ‘Open’.

B2 P11

B2 P12

  1. Drag the .sqlproj file you just opened into the “04 – Database” folder and right click that .sqlproj file to create sub-folders called “Tables” and “Scripts”.

B1 4 Tablescript

  1. Right click the Tables folder to add a Table and the Scripts folder to add a Script (Not in build) & Post Deployment Script.
  • Table is where the columns will be defined.
  • Scripts are where the data will be seeded (empty when you first publish the tables).
  • Post deployment script will run once the tables are deployed. This script includes simple commands to allow each of the seed scripts to run and you NEED this.

B2 P13

B2 P14

B2 P15

B2 P16

Examples

Below are examples of how each of the three files would look like.

B2 P17

Again, the script file is empty the first time you publish it. This is how the script would appear when you need to seed the data afterwards.

B2 P18

Simply add ‘:r .{script name}.sql. You can ignore the red lines with the Post Deployment script file if they appear.

B2 P19

That is all you have to do to add the database project to your .NET MVC Web Application!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

David Hwang

David Hwang is an Associate Technical Consultant in the Sitecore/Microsoft Business Unit. He will be blogging about various concepts in technologies for beginners.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram