Back-End Development

Database-First EF Setup on VS .NET MVC Framework – Initial Folder Structure

Why Sitecore Developers Choose Perficient for jobs

Hello all! Before you begin reading, keep in mind that this tutorial is designed for beginners. As such, it covers every step without skipping parts that might be obvious to some. This tutorial is made possible thanks to Josh Kostal, who taught me majority of what is covered in this post.

The reason you have to undergo such a tedious process to create the initial folder structure is because simply creating folders in the File Explorer will not be mirrored inside the Visual Studio. We also have to manually reconfigure the new path to the .csproj file after moving the application over to the new folder. You will see what I mean as we move forward in this tutorial!

Part 1 of this tutorial series covers the initial stage of the setup as described in the overview below.

Part 2 will include publishing the data in the tables to your local machine, reverse engineering to create the dbcontext, and adding other components such as providers and models in ’02 – Application’ and ’03 – Domain’ folders.

Overview of Part 1

  1. Create ASP.NET Web Application (.NET Framework).
  2. Create the folder structure in the File Explorer (01 – Presentation, 02 – Application, 03 – Domain, and 04 – Database).
  3. Transfer the application created in 1) to “01 – Presentation” folder.
  4. Reconfigure the path for .csproj file.
  5. Go into Visual Studio and create the Solution Folders that align with the folder structure in the File Explorer.
  6. Drag the original project into the “01 – Presentation” solution folder inside the Visual Studio.
  7. Create Sql server database project and configure the path to the “04 – Database” folder.
  8. Make adjustments in Visual Studio accordingly.
  9. Add scripts and table files.

Details

Creating the Folder Structure

1) Create a new ASP.NET Web Application (.NET Framework) using the MVC template. I will name the application ‘Demo_01’ in this example.

B2 P1

2) Once you create the project, go to where the project is located in the File Explorer. Inside the folder that contains the .sln file, create folders that will each hold the ‘Presentation’, ‘Application’, ‘Domain’, and ‘Database’ component.

B2 P2

3) Drag all the items in the application folder (source > repos > Demo_01 > Demo_01 in this example) into the “01 – Presentation” folder. The image below demonstrates all the items that were dragged and dropped (or copied and pasted) into the new folder.

B2 P3

4) Open .sln file with a Notepad++ (Notepad works too).

B2 P4

5) Edit the path for .csproj file (now that you have moved the .csproj file from the original application folder to the “01 – Presentation” folder). You can change where it is highlighted in the image below.

B2 P5

However, when you open up the .sln file, you are going to realize how the folders you created in the File Explorer are not reflected within Visual Studio. You will have to create solution folders separately inside Visual Studio.

 

Platforms & Technology - A Business Leaders Guide to Key Trends in Cloud
A Business Leaders Guide to Key Trends in Cloud

Cloud’s dynamic nature can make it hard to keep up with the wide-ranging capabilities that make it a key enabler to improve business processes and support a larger digital transformation.

Get the Guide

 

6) Right click the solution file and add a New Solution Folder (Note how it is NOT a regular New Folder).

B2 P6

7) Drag the project into the “01 – Presentation” folder and create other solution folders accordingly.

B2 P7

Adding the Database Project

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

B2 P8

9) 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

10) Right click the solution and Add Existing Project. Locate the SQL server database project you created in 8) and click ‘Open’.

B2 P11

B2 P12

11) Drag .sqlproj file you just open in 10) into “04 – Database” folder and right click that file (not the “04 – Database” folder) to create sub-folders called “Tables” and “Scripts”. Right click the Tables folder to Add a Table and the Scripts folder to Add a Script (Not in build). Table is where the columns will be defined and scripts where the data will be seeded. Refer to the images below.

B2 P13

B2 P14

B2 P15

12) Add a Post deployment script file under Scripts folder as well. 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 P16

Examples

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

B2 P17

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

B2 P18

B2 P19

That is it for the initial setup! In part 2, we will cover publishing the data to your local machine, reverse engineering to create the dbcontext, and adding other components such as providers and models in ’02 – Application’ and ’03 – Domain’ folders.

About the Author

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

Leave a Reply

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

Subscribe to the Weekly Blog Digest:

Sign Up