Hey guys.
So we have gone to quite some US loan with our project and though we want to start actually assigning
courses to students and moving along with actually having a school management system no one thing that
we didn't do before know is add a table and the requisite structure to support adding lecturers to the
courses because we have courses we have students and we have enrolments enrolments would be a student
being assigned to do a course and the grade that they would get.
But what we don't have is having the lecturer who would be teaching the course.
We do have a facility to manage that in our application.
As it stands.
So this video is geared towards just adding that new module and as you go along with your development
especially if you're doing it in an agile way as the need arises you may need to modify your application
to fit the need.
So that is the scenario that we want to highlight to be.
So we start by adding the lecturer table to our database of course.
So I have my default connection or school management so they use server explorer or use the skill object
explorer.
You should have the same effect.
SOMEONE TO USE MY WILL server object explorer and I'm going to add a new table to my school management
VB and this will be for lecturers.
So I added first name and last name to this database table of course whatever it is that you want to
store about a lecturer you just want to put in this table maybe for your school management system you'd
want to build out the whole profile for the security at had a profile picture maybe their speciality
whatever it is.
But for the sake of moving along I'm just going to leave it as is.
Those first name last name.
That's all we need for any given lecture at least right.
We also have to make sure that our I.D. column is auto incrementing so for ease easier in this regular
care and just include that identity and identity increment column and we would just tick identity to
make sure that its identity and make sure its counting up by one each time.
And once all of those changes have been made and you would see reflecting here in the codes or the euro
the code or you did it that way that's fine.
And then you can update this table and that's successful no another change we want to make is to the
enrolment stable because an enrolment essentially is assigning lecturer to a class with us students
and whatever agreed that lecture would have given that student for that class.
So that is how our enrolment table is set up so we want to make sure that we modify our enrolment table
to be aware that it should have another foreign key in the form of lecture I.D. So to add the foreign
key concerns to the enrolment stable.
I'm just entity the shortcut way and copy and paste one of the consonants that's already there and just
change the information according to my need.
So I want to see lecturer so I.D. and then we changed the table to lecturers and the I.D. column to
IAB and I made a mistake there.
So this would be I.D. since that is the primary key column over here so that we know its lecturer.
I do have to call it lecturer idea I mean it's clearly we don't have any problems and I'm getting an
error here because I don't have a comma so the comma and then all of those errors should disappear.
So that is adding the constraint what I need to do is add the roll or the column rather so a lecturer
I.D. to make sure it is integer because it's a foreign key and I I'm going to not alone No because it
doesn't make sense of an error enrolments with a course on student and no lecturer.
Right.
I can't actually leave this No because you could have the students enrolled into the course and we don't
yet know who is going to teach it.
So I know that everything is okay.
I have the lecture I call them and then I set up a constraint to see that it should apply to the lecture
I.D. column and reference the idea column in lectures and then all of that is in place so I just make
updates.
And that should update our database tables.
Now recall that we're doing database first development which means once the changes in the database
we have updates our model so we have to find or entity framework model which will just click here and
get this diagram and then we have a few of these that we expect we and we expect that the enrolment
table will build baited with a new column and that's a new table will be added to this model related
to the enrolment.
So I'm going to right click in the empty space and click Update model from database which would then
bring up this dialog box I can drill down into and select specifically the tables I wish to add.
And the reason for doing this would be I don't want to add migration history if you're doing code with
rolls and end users then fine you can do that but I'm not going to be doing any of those things I'm
only interested in my lecture is stable and then actually finish once that is done it does a full firstly
it refreshes the enrolment table and adds in the missing column and it also adds in my lecture table
and shows the relationship between them.
Also notice because I load no the notation here is different from the notation here now that I have
my lecture too but added the next step is to make the crowd operations available so I'm just going to
go to controllers click Add add new controller and then I'm going to continue with the MVC five controller
with entity framework I'm going to specify that I want this controller to be based on the lectures table
which is not listed.
And if you run into this problem it's because we didn't build.
So every time you make a change to the model I think I said that before you always do A build somewhat
to control shift and B and just do a quick build and after a successful build we can try that operation
again so we ride controllers go to add new controller continue with the entity framework based controller
find or model so I can actually just type like sure and there we go.
So our model is not available and we can leave everything else as is.
And we just go ahead and take
once that operation is complete.
We will land on this controller with everything already wired up and we've already gone through fulfilled.
This is we get our listing off all the lectures the detail per lecture which would be like his profile
page.
We get to create and edit and delete so we know that our CRUD operations for any given lecture is completed.
What do you want to do is probably add a few test lectures so that we can continue with the enrollments
and additionally I'm going to just adjust my male page to make sure that I can easily get to my lecture
space I'm just going to add another link here controls CMB and this will be for lecturers so that when
we put that link we will be in that lecture as CRUD operations space.
Right.
So I'm just going to quickly run and show you what that looks like.
Given the way it should be very familiar.
All right so here is our page and we see that our lectures link is no added and we can browse through
it.
And I added a few lectures here Jack Daniels Johnny Walker and re nephew.
So that's it for adding the lecture management module to our application and of course the last up for
this video is the check in this code so that we can make sure that we pick up where we left off should
anything go wrong.
So I'm just gonna write a quick message and then we just come it all and sync sheet committing our changes
locally and on github for our enrollment management.
We're going to repeat a lot of the steps we just went through with our lecturers portion but then a
lot more adjustments are going to be made so that we can have a nice dynamic and user friendly application
at the end of it someone to start off with adding a new controller and leaving it with entity framework.
And this time we're going to do it for our enrollments model.
So we went through what we did for everything but enrollments.
So we create that ad.
All right.
So far our enrollments control that we're going to just take some time to appreciate what's going on
here because this table is going to be a bit more unique than all the other tables that we added controllers
for and because of that there are a few more functions that are going to be here that you probably didn't
see before.
No.
So firstly for our index page we see here that we get a slightly different query and I'm going to bring
up our lecturer as controller so we can compare for the lecturer controller.
All we have to do is get all the lectures or IDB the lectures the list and we just return that.
However you'd notice that we have two lines in enrollments and we're declaring a variable called enrollments
which is running a similar or getting the D.B. from the context getting the enrollments but then we're
doing something called include and walking through does is it initiates what we call eager loading which
includes literally any related entity to this table.
So in other words if we look back at our model really quickly we see that we have three late relations
to enrollment and I'm just going to drag it across the screen so we can see it differently.
All right so in our area and just diagram you see that the lines follow and enrollments.
That's because the lecturer is associated with enrollments.
The students is solicited with romance and the course is associated with enrollment which means if we
look at an enrollment we should be seeing data for the lecturer the course and the students.
And so when the controller is being generated is seeking tokens all of these relationships and it is
writing the query that says that you should include data for the related course you should include data
for the related students and you should also include data for the related lecturer.
So each line of enrollment is going to have data associated with the course the student and the lecturer.
So when we load index we can look at that a bit more and then if we look at creates then we'll see that
it is creating some generic variable which is in the form of what we call a view bug.
And you would've seen view bug for a bit in a previous lecture but view bug is just something that is
created on the fly.
And what we're assigning to it is a dropdown list which see sharp or speed up meant MVC calls a select
list and we're loading it with the list of courses and we're associating this select list the I.D. or
the value should be the ideas off the course.
And what is to be displayed would be the title.
All right we're doing this three times so we're doing this for of course I.D. for students and for a.
So for each relationship we probably want to have a dropdown list that's will allow us to select the
value that we want to create that relationship with.
And as we go along we'll see that all it does is take all of those ideas values and create an enrollment
record.
The same would be for Ed as you know that edits and creates share a lot of the code and that's an area
if you remove on enrollments then you just remove an enrollment so deletes wouldn't look much different.
So we can just go ahead and start and see what our enrollments Module looks like before we do that.
Of course we want to add a link to our little page so that we can get through it easily.
We're going to quickly hop on over to shared and layouts and then the students will once again duplicate
this line in the same B and I'm just going to take all the pages we're not using so I'm not using a
bolt on contact.
So I'm just going to remove those from the menu.
So we have more space for the pages that really matter.
All right so no I can just navigate and we'll check out how enrollments looks.
So when our visuals we can just click on enrollments and we'll see the list that is there so far and
I had added these directly to the database so I went in and I said student 1 is assigned to course one
et cetera et cetera.
So that's why you would see those things here as you can see the table would take some modifications
because well great is no title is there.
The last name of the student loan is there and the first name is there but that's not necessarily how
we want our labels to look.
And that's not necessarily the data that we want to present here.
If we look at create we see that we get our three dropdown boxes so we get our grade here which was
text we get of course I.D. which gives us that dropped on list.
We get double this for our students and we get a drop on this for all of the last names of our lecturers.
So we see that we need to do a bit of modification what we're aiming for really is to re engineer this
page.
So when we get back we'll look at how we can go both modify all of these things.