17.4. Exercises: OMG the ORM!¶
For the exercises, we are going to continue building on our CodingEvents
application.
The exercise instructions assume that your code resembles the persistent-controller branch.
Create a new branch in your own CodingEvents
repo to get started on the exercises.
Note
You will be making one model class and adding to DbContext
.
If you are not sure what these classes should look like, refer back to the section on data stores and DbContext
.
17.4.1. The EventCategory
Class¶
First, create a new class called EventCategory
in the Models
directory.
EventCategory
needs to have the following:
An
Id
property of typeint
.A
Name
property of typestring
.A no arg constructor.
A constructor that sets the value of
Name
.
EventCategory
represents data that will be stored in our database.
17.4.2. Adding to DbContext
¶
Once you have created EventCategory
, you need to add to EventDbContext
to set up a table in the database for storing categories.
17.4.3. Adding a New Table to the Database¶
With the two steps completed above, you now need to add a new migration and update your database. You should see a new table in your database!
Tip
Where is my table!?
If you are not able to see your new EventCategory
table try the following:
Drop tables
Start by dropping all of your tables in MySql Workbench
Add a new migration in your terminal
Update your database in the terminal
Refresh your schema in MySql Workbench
If you still can’t see your table, then you will need to drop your schema and delete old migrations.
Drop schema
Drop the schema in MySql Workbench. Don’t worry, we’ll get it back.
Recreate your schema in MySql Workbench like you did earlier. You can use the same names and passwords if you like. If you change any of the names and passwords, make sure you update your MySql connections that you made in the
appsettings.json
file as well as either youProgram.cs
orStartup.cs
files.Delete your old migrations from your C# project.
Add a new migration in your terminal
Update your database in the terminal
Refresh your schema in MySql Workbench
You should see your new table in MySql Workbench now.
17.4.4. The EventCategoryController
Class¶
Create EventCategoryController
in the Controllers
directory.
To get our action methods working, we also need a variable of type EventDbContext
.
For now, we are just going to set up our Index()
action method so that it displays all of the categories in our table.
17.4.4.1. Index()
¶
Index()
needs to do the following:
Responds to
GET
requests atEventCategory/Index
and returns a view calledIndex.cshtml
.Pass the
DbContext
category values as a list into the view template as a model.
17.4.5. Adding a View¶
Create an EventCategory
directory inside of our Views
directory.
Add a new view called Index.cshtml
.
Index.cshtml
needs to have the following:
Use the list passed in from the action method in the controller as a model to populate the view.
An
h1
with an appropriate heading for the page.A table that will display all of the category names of the event categories stored in our database.