1 00:00:00,180 --> 00:00:01,140 Hey guys. 2 00:00:01,140 --> 00:00:08,520 So we have gone to quite some US loan with our project and though we want to start actually assigning 3 00:00:08,820 --> 00:00:15,990 courses to students and moving along with actually having a school management system no one thing that 4 00:00:15,990 --> 00:00:23,640 we didn't do before know is add a table and the requisite structure to support adding lecturers to the 5 00:00:23,640 --> 00:00:28,620 courses because we have courses we have students and we have enrolments enrolments would be a student 6 00:00:28,620 --> 00:00:31,760 being assigned to do a course and the grade that they would get. 7 00:00:31,770 --> 00:00:36,540 But what we don't have is having the lecturer who would be teaching the course. 8 00:00:36,720 --> 00:00:39,920 We do have a facility to manage that in our application. 9 00:00:39,930 --> 00:00:40,900 As it stands. 10 00:00:41,010 --> 00:00:46,410 So this video is geared towards just adding that new module and as you go along with your development 11 00:00:46,440 --> 00:00:51,480 especially if you're doing it in an agile way as the need arises you may need to modify your application 12 00:00:51,480 --> 00:00:52,640 to fit the need. 13 00:00:52,650 --> 00:00:56,760 So that is the scenario that we want to highlight to be. 14 00:00:56,790 --> 00:01:00,630 So we start by adding the lecturer table to our database of course. 15 00:01:00,630 --> 00:01:06,930 So I have my default connection or school management so they use server explorer or use the skill object 16 00:01:07,190 --> 00:01:07,920 explorer. 17 00:01:07,950 --> 00:01:09,320 You should have the same effect. 18 00:01:09,450 --> 00:01:15,710 SOMEONE TO USE MY WILL server object explorer and I'm going to add a new table to my school management 19 00:01:15,720 --> 00:01:18,870 VB and this will be for lecturers. 20 00:01:18,870 --> 00:01:24,180 So I added first name and last name to this database table of course whatever it is that you want to 21 00:01:24,180 --> 00:01:28,620 store about a lecturer you just want to put in this table maybe for your school management system you'd 22 00:01:28,620 --> 00:01:34,010 want to build out the whole profile for the security at had a profile picture maybe their speciality 23 00:01:34,020 --> 00:01:34,760 whatever it is. 24 00:01:34,770 --> 00:01:39,480 But for the sake of moving along I'm just going to leave it as is. 25 00:01:39,480 --> 00:01:40,980 Those first name last name. 26 00:01:41,010 --> 00:01:44,260 That's all we need for any given lecture at least right. 27 00:01:45,120 --> 00:01:51,780 We also have to make sure that our I.D. column is auto incrementing so for ease easier in this regular 28 00:01:51,780 --> 00:01:59,040 care and just include that identity and identity increment column and we would just tick identity to 29 00:01:59,040 --> 00:02:03,100 make sure that its identity and make sure its counting up by one each time. 30 00:02:03,390 --> 00:02:09,880 And once all of those changes have been made and you would see reflecting here in the codes or the euro 31 00:02:09,970 --> 00:02:12,310 the code or you did it that way that's fine. 32 00:02:12,480 --> 00:02:19,020 And then you can update this table and that's successful no another change we want to make is to the 33 00:02:19,020 --> 00:02:27,720 enrolment stable because an enrolment essentially is assigning lecturer to a class with us students 34 00:02:28,050 --> 00:02:31,930 and whatever agreed that lecture would have given that student for that class. 35 00:02:31,980 --> 00:02:39,720 So that is how our enrolment table is set up so we want to make sure that we modify our enrolment table 36 00:02:39,720 --> 00:02:48,780 to be aware that it should have another foreign key in the form of lecture I.D. So to add the foreign 37 00:02:48,780 --> 00:02:51,880 key concerns to the enrolment stable. 38 00:02:51,920 --> 00:02:59,880 I'm just entity the shortcut way and copy and paste one of the consonants that's already there and just 39 00:02:59,880 --> 00:03:02,200 change the information according to my need. 40 00:03:02,190 --> 00:03:14,070 So I want to see lecturer so I.D. and then we changed the table to lecturers and the I.D. column to 41 00:03:14,710 --> 00:03:16,500 IAB and I made a mistake there. 42 00:03:16,500 --> 00:03:23,180 So this would be I.D. since that is the primary key column over here so that we know its lecturer. 43 00:03:23,180 --> 00:03:29,370 I do have to call it lecturer idea I mean it's clearly we don't have any problems and I'm getting an 44 00:03:29,370 --> 00:03:37,560 error here because I don't have a comma so the comma and then all of those errors should disappear. 45 00:03:37,590 --> 00:03:45,510 So that is adding the constraint what I need to do is add the roll or the column rather so a lecturer 46 00:03:45,510 --> 00:03:53,220 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 47 00:03:53,220 --> 00:03:58,190 doesn't make sense of an error enrolments with a course on student and no lecturer. 48 00:03:58,340 --> 00:03:59,210 Right. 49 00:03:59,610 --> 00:04:05,250 I can't actually leave this No because you could have the students enrolled into the course and we don't 50 00:04:05,250 --> 00:04:06,990 yet know who is going to teach it. 51 00:04:07,110 --> 00:04:08,960 So I know that everything is okay. 52 00:04:09,030 --> 00:04:15,270 I have the lecture I call them and then I set up a constraint to see that it should apply to the lecture 53 00:04:15,270 --> 00:04:21,840 I.D. column and reference the idea column in lectures and then all of that is in place so I just make 54 00:04:21,900 --> 00:04:22,700 updates. 55 00:04:22,890 --> 00:04:26,370 And that should update our database tables. 56 00:04:26,370 --> 00:04:30,690 Now recall that we're doing database first development which means once the changes in the database 57 00:04:30,690 --> 00:04:36,480 we have updates our model so we have to find or entity framework model which will just click here and 58 00:04:36,480 --> 00:04:42,660 get this diagram and then we have a few of these that we expect we and we expect that the enrolment 59 00:04:42,660 --> 00:04:47,880 table will build baited with a new column and that's a new table will be added to this model related 60 00:04:47,880 --> 00:04:48,750 to the enrolment. 61 00:04:48,780 --> 00:04:55,290 So I'm going to right click in the empty space and click Update model from database which would then 62 00:04:55,290 --> 00:05:02,000 bring up this dialog box I can drill down into and select specifically the tables I wish to add. 63 00:05:02,100 --> 00:05:07,530 And the reason for doing this would be I don't want to add migration history if you're doing code with 64 00:05:07,530 --> 00:05:13,320 rolls and end users then fine you can do that but I'm not going to be doing any of those things I'm 65 00:05:13,320 --> 00:05:19,630 only interested in my lecture is stable and then actually finish once that is done it does a full firstly 66 00:05:19,700 --> 00:05:26,070 it refreshes the enrolment table and adds in the missing column and it also adds in my lecture table 67 00:05:26,100 --> 00:05:28,370 and shows the relationship between them. 68 00:05:28,560 --> 00:05:35,730 Also notice because I load no the notation here is different from the notation here now that I have 69 00:05:35,730 --> 00:05:42,030 my lecture too but added the next step is to make the crowd operations available so I'm just going to 70 00:05:42,030 --> 00:05:50,430 go to controllers click Add add new controller and then I'm going to continue with the MVC five controller 71 00:05:50,430 --> 00:05:59,180 with entity framework I'm going to specify that I want this controller to be based on the lectures table 72 00:05:59,840 --> 00:06:01,490 which is not listed. 73 00:06:01,490 --> 00:06:04,790 And if you run into this problem it's because we didn't build. 74 00:06:04,790 --> 00:06:10,630 So every time you make a change to the model I think I said that before you always do A build somewhat 75 00:06:10,630 --> 00:06:16,910 to control shift and B and just do a quick build and after a successful build we can try that operation 76 00:06:16,910 --> 00:06:23,900 again so we ride controllers go to add new controller continue with the entity framework based controller 77 00:06:24,800 --> 00:06:30,060 find or model so I can actually just type like sure and there we go. 78 00:06:30,080 --> 00:06:35,700 So our model is not available and we can leave everything else as is. 79 00:06:35,720 --> 00:06:38,040 And we just go ahead and take 80 00:06:40,780 --> 00:06:42,580 once that operation is complete. 81 00:06:42,580 --> 00:06:49,110 We will land on this controller with everything already wired up and we've already gone through fulfilled. 82 00:06:49,160 --> 00:06:55,600 This is we get our listing off all the lectures the detail per lecture which would be like his profile 83 00:06:55,600 --> 00:06:56,380 page. 84 00:06:56,470 --> 00:07:05,100 We get to create and edit and delete so we know that our CRUD operations for any given lecture is completed. 85 00:07:05,110 --> 00:07:10,960 What do you want to do is probably add a few test lectures so that we can continue with the enrollments 86 00:07:11,260 --> 00:07:17,900 and additionally I'm going to just adjust my male page to make sure that I can easily get to my lecture 87 00:07:17,900 --> 00:07:25,300 space I'm just going to add another link here controls CMB and this will be for lecturers so that when 88 00:07:25,300 --> 00:07:30,220 we put that link we will be in that lecture as CRUD operations space. 89 00:07:30,240 --> 00:07:30,490 Right. 90 00:07:30,940 --> 00:07:35,160 So I'm just going to quickly run and show you what that looks like. 91 00:07:35,380 --> 00:07:37,070 Given the way it should be very familiar. 92 00:07:37,440 --> 00:07:43,960 All right so here is our page and we see that our lectures link is no added and we can browse through 93 00:07:43,960 --> 00:07:44,790 it. 94 00:07:45,010 --> 00:07:51,430 And I added a few lectures here Jack Daniels Johnny Walker and re nephew. 95 00:07:51,430 --> 00:07:58,330 So that's it for adding the lecture management module to our application and of course the last up for 96 00:07:58,330 --> 00:08:05,980 this video is the check in this code so that we can make sure that we pick up where we left off should 97 00:08:05,980 --> 00:08:07,000 anything go wrong. 98 00:08:07,000 --> 00:08:14,530 So I'm just gonna write a quick message and then we just come it all and sync sheet committing our changes 99 00:08:14,530 --> 00:08:20,010 locally and on github for our enrollment management. 100 00:08:20,020 --> 00:08:26,620 We're going to repeat a lot of the steps we just went through with our lecturers portion but then a 101 00:08:26,620 --> 00:08:33,010 lot more adjustments are going to be made so that we can have a nice dynamic and user friendly application 102 00:08:33,280 --> 00:08:40,700 at the end of it someone to start off with adding a new controller and leaving it with entity framework. 103 00:08:40,870 --> 00:08:45,360 And this time we're going to do it for our enrollments model. 104 00:08:45,430 --> 00:08:48,840 So we went through what we did for everything but enrollments. 105 00:08:48,850 --> 00:08:51,750 So we create that ad. 106 00:08:51,910 --> 00:08:52,270 All right. 107 00:08:52,270 --> 00:08:58,360 So far our enrollments control that we're going to just take some time to appreciate what's going on 108 00:08:58,360 --> 00:09:04,300 here because this table is going to be a bit more unique than all the other tables that we added controllers 109 00:09:04,300 --> 00:09:09,670 for and because of that there are a few more functions that are going to be here that you probably didn't 110 00:09:09,670 --> 00:09:10,520 see before. 111 00:09:10,510 --> 00:09:11,060 No. 112 00:09:11,080 --> 00:09:16,420 So firstly for our index page we see here that we get a slightly different query and I'm going to bring 113 00:09:16,480 --> 00:09:20,850 up our lecturer as controller so we can compare for the lecturer controller. 114 00:09:20,860 --> 00:09:27,850 All we have to do is get all the lectures or IDB the lectures the list and we just return that. 115 00:09:27,940 --> 00:09:34,300 However you'd notice that we have two lines in enrollments and we're declaring a variable called enrollments 116 00:09:34,570 --> 00:09:41,320 which is running a similar or getting the D.B. from the context getting the enrollments but then we're 117 00:09:41,320 --> 00:09:50,110 doing something called include and walking through does is it initiates what we call eager loading which 118 00:09:50,260 --> 00:09:56,020 includes literally any related entity to this table. 119 00:09:56,020 --> 00:10:02,840 So in other words if we look back at our model really quickly we see that we have three late relations 120 00:10:02,840 --> 00:10:07,620 to enrollment and I'm just going to drag it across the screen so we can see it differently. 121 00:10:07,630 --> 00:10:11,410 All right so in our area and just diagram you see that the lines follow and enrollments. 122 00:10:11,410 --> 00:10:14,990 That's because the lecturer is associated with enrollments. 123 00:10:15,130 --> 00:10:21,640 The students is solicited with romance and the course is associated with enrollment which means if we 124 00:10:21,640 --> 00:10:28,970 look at an enrollment we should be seeing data for the lecturer the course and the students. 125 00:10:28,990 --> 00:10:35,230 And so when the controller is being generated is seeking tokens all of these relationships and it is 126 00:10:35,230 --> 00:10:42,100 writing the query that says that you should include data for the related course you should include data 127 00:10:42,100 --> 00:10:47,320 for the related students and you should also include data for the related lecturer. 128 00:10:47,320 --> 00:10:55,120 So each line of enrollment is going to have data associated with the course the student and the lecturer. 129 00:10:55,120 --> 00:11:01,780 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 130 00:11:01,810 --> 00:11:07,930 it is creating some generic variable which is in the form of what we call a view bug. 131 00:11:07,930 --> 00:11:14,620 And you would've seen view bug for a bit in a previous lecture but view bug is just something that is 132 00:11:14,620 --> 00:11:16,270 created on the fly. 133 00:11:16,270 --> 00:11:23,650 And what we're assigning to it is a dropdown list which see sharp or speed up meant MVC calls a select 134 00:11:23,650 --> 00:11:31,540 list and we're loading it with the list of courses and we're associating this select list the I.D. or 135 00:11:31,540 --> 00:11:34,610 the value should be the ideas off the course. 136 00:11:34,780 --> 00:11:38,190 And what is to be displayed would be the title. 137 00:11:38,260 --> 00:11:43,300 All right we're doing this three times so we're doing this for of course I.D. for students and for a. 138 00:11:43,300 --> 00:11:50,590 So for each relationship we probably want to have a dropdown list that's will allow us to select the 139 00:11:50,590 --> 00:11:54,590 value that we want to create that relationship with. 140 00:11:54,640 --> 00:12:01,930 And as we go along we'll see that all it does is take all of those ideas values and create an enrollment 141 00:12:01,930 --> 00:12:02,860 record. 142 00:12:02,860 --> 00:12:08,180 The same would be for Ed as you know that edits and creates share a lot of the code and that's an area 143 00:12:08,260 --> 00:12:13,240 if you remove on enrollments then you just remove an enrollment so deletes wouldn't look much different. 144 00:12:13,570 --> 00:12:19,150 So we can just go ahead and start and see what our enrollments Module looks like before we do that. 145 00:12:19,150 --> 00:12:24,460 Of course we want to add a link to our little page so that we can get through it easily. 146 00:12:24,480 --> 00:12:31,180 We're going to quickly hop on over to shared and layouts and then the students will once again duplicate 147 00:12:31,180 --> 00:12:37,000 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 148 00:12:37,000 --> 00:12:37,990 bolt on contact. 149 00:12:37,990 --> 00:12:39,880 So I'm just going to remove those from the menu. 150 00:12:39,910 --> 00:12:43,690 So we have more space for the pages that really matter. 151 00:12:43,690 --> 00:12:48,730 All right so no I can just navigate and we'll check out how enrollments looks. 152 00:12:48,760 --> 00:12:56,140 So when our visuals we can just click on enrollments and we'll see the list that is there so far and 153 00:12:56,320 --> 00:13:02,710 I had added these directly to the database so I went in and I said student 1 is assigned to course one 154 00:13:02,710 --> 00:13:03,460 et cetera et cetera. 155 00:13:03,460 --> 00:13:09,850 So that's why you would see those things here as you can see the table would take some modifications 156 00:13:09,850 --> 00:13:13,390 because well great is no title is there. 157 00:13:13,720 --> 00:13:20,070 The last name of the student loan is there and the first name is there but that's not necessarily how 158 00:13:20,080 --> 00:13:21,510 we want our labels to look. 159 00:13:21,670 --> 00:13:25,290 And that's not necessarily the data that we want to present here. 160 00:13:25,330 --> 00:13:32,260 If we look at create we see that we get our three dropdown boxes so we get our grade here which was 161 00:13:32,260 --> 00:13:36,630 text we get of course I.D. which gives us that dropped on list. 162 00:13:36,700 --> 00:13:43,880 We get double this for our students and we get a drop on this for all of the last names of our lecturers. 163 00:13:43,880 --> 00:13:50,970 So we see that we need to do a bit of modification what we're aiming for really is to re engineer this 164 00:13:50,970 --> 00:13:51,480 page. 165 00:13:51,660 --> 00:13:56,940 So when we get back we'll look at how we can go both modify all of these things.