Homework five tasks us with creating a simple database using a T-SQL script and connect to it with some basic options. For me, learning T-SQL was the most difficult part as I had a rough time finding good information on the subject. Essentially, we needed to create a basic database with a few entrys that were automatically loaded from the script. Then, be able to add more through a create form and return to the index and see the added entries. This program will theoretically be used to change addresses for people registered through the DMV.
These scripts are pretty simple once you actually figure out what your doing. The up.sql script below creates the database, creates the table, and adds the first 5 entries. What makes this technique useful is that you can run this script on other machines to start up the same project on multiple machines. The down.sql script just detaches the database, then deletes it.
I added the DbContext class as shown below, its a simple class that just creates a set and get for our Request Controller. I also added another automatic dbcontext generator class. However, in class I learned how to install from the package installer command line.
The model class is basically like creating an interface to use with the database. You can redefine stuff here if needed. or just set requirements on fields as shown below.
After quite a bit of fiddling I found the correct connection string to use here. Unfortunately, syntax wise it is hard to tell if you are doing this correct the first time. I accidently started by connecting to the SQL server connection string instead of the database that I should have been connecting too. Instead of throwing an understandable error of not being connected, it kept throwing a nullarguementexception which I later found out that it wasn't connecting! After adding the correct string everything worked much better.
The home controller for this project was very simple. It just returned an index view. The request controller is where much of the work is done for creating the views. The request index view just shows a table of what all is inside the database. The request create view is just a form that the user can input data too.
This view just links the user to the proper request controller.
This page displays the information in the Database and nicely outputs the results to a table. It was auto generated by MVC with a few minor adjustments.
The Create form is represented below. I used Razor code to help create this UI as shown. Essentially, everything below is just a form that takes the user input to change an address through the DMV system. It was auto generated by MVC and has minor changes where necessary.
In conclusion, working with a database can be extremely useful and most projects nowadays require one. Learning to use a database through MVC has been a learning experience but I am glad I did it. I am looking forward to learning more about entity framework in the next few homeworks to fully understand it.
Link to: Homework Five
Link to: Repository for hw5