Welcome to Part 2 of the tutorial! Now that we have the table, scripts, and post-deployment scripts from Part 1, we are going to publish the table to our local server, reverse engineer to create the Dbcontext, and add other components like providers and models (DTOs and View Models).
The final structure we will have at the end of this tutorial:
Publish Database and Reverse Engineer
1) Right-click Demo_01.Database (SQL Server Database Project) and select ‘Publish’ to publish the data in VS to your local machine. Click ‘Edit’ and locate the server you want to publish the database to. Browse and type ‘.’ for a local server.
2) Click the “Advanced…” option and select the “Always re-create database” option. With this option, Visual Studio will drop the current database and re-create the new one every time you publish tables with recent changes.
3) Click “Save Profile As…” and save .publish.xml file the first time you set up. You will then find this .xml file, which you can simply double-click to publish for any changes in the future.
Once you click ‘Publish,’ you will notice a table with data added to your local database in your SQL Server Management Studio (SSMS)!
4) Now we are going to create a Class Library (.NET Framework) that will contain the DbContext that corresponds to the tables we created in the SQL Server Database Project (Demo_01.Database).
5) Name it.Data (a naming convention you could follow) and set the location path to the “04 – Database” folder.
Once you create the project, head back out to the Demo_01.sln that we had been in. Repeat 10) of Part 1 of this tutorial to ‘Add Existing Project’ and drag the newly created file under the “04 – Database” folder.
At this point, the folder structure would look like this.
6) Before we can reverse-engineer, we need to download EF Core Power Tools. You can download it here. Right-click Demo_01.Data (Class Library) and select ‘Reverse Engineer.’
7) Select Demo_01.Database (SQL Server Project), check ‘Use EF Core’ and click ‘OK.’
8) Choose the table you created and click ‘OK.’ Name your DbContext as you wish and click ‘OK.’
9) You can observe how a model is automatically configured using ModelBuilder and a DbSet that mirrors the table we created earlier.
10) Here is the Model created for us. We can remove the Class1.cs file that was auto-generated.
That is all we need to set up the database part of our solution! Now we are going to move on to set up providers in the Application folder and models (DTOs) and view models in the Domain folder.
Adding Providers and Models
Providers are where the DbContext will be called, and models (DTOs) will be populated with data from it. We will create Class Libraries for a Demo_01. Application project and a Demo_01.Domain project and place it under ’02 – Application’ and ’03 – Domain’ folders, respectively (Refer to Step 4 and 5 of this tutorial).
11) Create a Class Library (.NET Framework) like we did in 4) that will later hold providers. You could append. Application to the project name. Don’t forget to set your location to the ’02 – Application’ folder.
12) Create another Class Library that will hold our models (DTOs) and View Models. You could append. Domain to the project name. Again, configure the location to ’03 – Domain’ folder.
13) Return to the Demo_01.sln project and right-click the solution and add existing projects. (Refer to 10. of Part 1 of this tutorial). Drag the added tasks into the corresponding folders.
14) ’02 – Application’ folder will contain Interfaces and Providers folders while ’03 – Domain’ holds Models and ViewModels folders.
Now that we have the entire structure constructed for the solution, it is time to populate our models with data! I will go over those in the following tutorials to come. Thank you for reading!