1 00:00:00,230 --> 00:00:04,120 In this video, we'll be completing the first part of the setup process, 2 00:00:04,120 --> 00:00:06,060 adding the class library project. 3 00:00:06,060 --> 00:00:09,200 I've already downloaded the ComicBookLibraryManager console app 4 00:00:09,200 --> 00:00:14,640 project files and opened the solution in Visual Studio Community 2017. 5 00:00:14,640 --> 00:00:17,020 If you're following along, see the teacher's notes for 6 00:00:17,020 --> 00:00:21,940 a link to download the files, or for more information about Visual Studio. 7 00:00:21,940 --> 00:00:23,660 Before we make any changes, 8 00:00:23,660 --> 00:00:27,290 let's run the app to ensure that everything works as expected. 9 00:00:27,290 --> 00:00:32,170 The first time that you run the app, EF will check the default SQL Server LocalDB 10 00:00:32,170 --> 00:00:36,740 instance to see if the ComicBookLibraryManager database exists. 11 00:00:36,740 --> 00:00:41,130 And if it doesn't exist, EF will create and see the database. 12 00:00:41,130 --> 00:00:44,970 If you run into any database related issues, see the teacher's notes for 13 00:00:44,970 --> 00:00:49,940 resources that will help you verify that SQL Server LocalDB is installed and 14 00:00:49,940 --> 00:00:52,650 configured correctly for use with EF. 15 00:00:52,650 --> 00:00:55,820 Now that we've verified that the console app is working correctly, 16 00:00:55,820 --> 00:00:58,080 let's add the class library project. 17 00:00:58,080 --> 00:01:03,230 In this Solution Explorer window, notice that the ComicBookLibraryManager project 18 00:01:03,230 --> 00:01:08,900 is located underneath the Visual Studio Solution named, ComicBookLibraryManager. 19 00:01:08,900 --> 00:01:13,880 A solution can contain one or more projects, to add a new project 20 00:01:13,880 --> 00:01:20,190 right click on the solution and select Add>New project. 21 00:01:20,190 --> 00:01:24,910 If you select the Visual C# category on the left, you'll see that there's 22 00:01:24,910 --> 00:01:29,120 more than one project type that contains the name Class Library. 23 00:01:29,120 --> 00:01:30,380 For our solution, 24 00:01:30,380 --> 00:01:35,110 we want the project type that is named Class library (.NET Framework). 25 00:01:35,110 --> 00:01:37,880 For more information about the .NET standard and 26 00:01:37,880 --> 00:01:42,030 the .NET standard Class Library project type, see the teacher's notes. 27 00:01:42,030 --> 00:01:44,160 Name the project ComicBookShared. 28 00:01:46,020 --> 00:01:50,600 Verify that the selected .NET Framework version is set 29 00:01:50,600 --> 00:01:55,479 to .NET Framework 4.6.1, then click the OK button. 30 00:01:55,479 --> 00:02:01,530 The project template adds a file in the root of the project name Class1.cs. 31 00:02:01,530 --> 00:02:03,170 Go ahead and delete that file. 32 00:02:05,237 --> 00:02:10,170 If we open the packages.config file in the root of the console app project, 33 00:02:10,170 --> 00:02:13,562 we can see that the project already has a reference to 34 00:02:13,562 --> 00:02:16,060 the EntityFramework NuGet package. 35 00:02:16,060 --> 00:02:20,140 Since we're moving our EF related classes to the Class Library project, 36 00:02:20,140 --> 00:02:24,650 it also needs to have reference to the EntityFramework NuGet package. 37 00:02:24,650 --> 00:02:29,130 To do that, right-click on the class library project in Solution Explorer, and 38 00:02:29,130 --> 00:02:32,390 select Manage NuGet Packages. 39 00:02:32,390 --> 00:02:35,690 Select the Browse tab, if it's not already selected, and 40 00:02:35,690 --> 00:02:37,810 install the EntityFramework package 41 00:02:46,990 --> 00:02:51,660 Go ahead and close all of the open tabs once the package is finished installing. 42 00:02:51,660 --> 00:02:56,220 Now we're ready to move the EF related classes to the Class Library project. 43 00:02:56,220 --> 00:03:00,590 The Data folder contains the Context, DatabaseInitializer and 44 00:03:00,590 --> 00:03:06,330 Repository classes, while the Models folder contains all of our indie classes. 45 00:03:06,330 --> 00:03:10,320 Visual Studio makes copying folders between projects easy to do. 46 00:03:10,320 --> 00:03:12,170 Simply drag and drop the Data and 47 00:03:12,170 --> 00:03:16,800 Models folders from the console app project to the Class Library project. 48 00:03:16,800 --> 00:03:20,360 Notice that I'm dragging the folders onto the name of the project, 49 00:03:20,360 --> 00:03:23,760 in order to copy the folders into the root of the project. 50 00:03:23,760 --> 00:03:25,430 We no longer need the Data and 51 00:03:25,430 --> 00:03:29,170 Models folder in the console app project, so go ahead and delete them. 52 00:03:31,270 --> 00:03:35,540 If we open the Context.cs file, we'll notice that the name space still 53 00:03:35,540 --> 00:03:39,860 reflects the file's original project: ComicBookLibraryManager. 54 00:03:39,860 --> 00:03:43,620 Typically the first part of the name space matches the name of the project that 55 00:03:43,620 --> 00:03:45,710 the files are contained within. 56 00:03:45,710 --> 00:03:50,690 This isn't a hard requirement, we could leave the name space as it is. 57 00:03:50,690 --> 00:03:55,510 But I like to keep my code clean, and it's simple enough to do, so let's fix it. 58 00:03:55,510 --> 00:03:58,520 Select the text ComicBookLibraryManager and 59 00:03:58,520 --> 00:04:02,060 press Ctrl+H to open the Quick Replace dialog. 60 00:04:02,060 --> 00:04:05,300 Set the replacement term to ComicBookShared and 61 00:04:05,300 --> 00:04:07,768 the scope of the replacement to the Current Project. 62 00:04:07,768 --> 00:04:12,440 Then press Alt+R to replace references one at a time or 63 00:04:12,440 --> 00:04:17,531 Alt+A to replace them all at once, which is what I'll do. 64 00:04:19,243 --> 00:04:22,345 Press Ctrl+Shift+S to save all the open files. 65 00:04:22,345 --> 00:04:25,315 Now, let's update the console app project, 66 00:04:25,315 --> 00:04:29,900 first we need to add a reference to the Class Library project. 67 00:04:29,900 --> 00:04:33,370 Right-click on References and select Add Reference. 68 00:04:33,370 --> 00:04:38,520 On the left, make sure that projects is selected, then select the ComicBookShared 69 00:04:38,520 --> 00:04:45,648 project and click OK. 70 00:04:45,648 --> 00:04:47,390 In the Program.cs file, 71 00:04:47,390 --> 00:04:51,480 we can see here in the scroll bar there are numerous compilation errors. 72 00:04:51,480 --> 00:04:56,370 That's what these red areas represent, we can right-click on the scroll bar, select 73 00:04:56,370 --> 00:05:01,780 Scroll Bar Options and set the vertical scroll bar behavior to use map mode. 74 00:05:01,780 --> 00:05:06,600 Now we can just hover over one of the red areas to see a pop-up preview of the code 75 00:05:06,600 --> 00:05:15,850 in that part of the file Artist, Role has squiggles, ComicBook has red squiggles. 76 00:05:15,850 --> 00:05:19,120 Hm, looks like the compiler is having trouble locating 77 00:05:19,120 --> 00:05:22,870 all of the types that we moved to the Class Library project. 78 00:05:22,870 --> 00:05:24,997 Let's take a look at our using statements. 79 00:05:28,312 --> 00:05:32,335 Need to update a couple of using statements here at the top of the file. 80 00:05:32,335 --> 00:05:39,870 Using ComicBookLibraryManager.Data, and using ComicBookLibraryManager.Models. 81 00:05:39,870 --> 00:05:44,932 Both Data and Models are now located in the ComicBookShared project. 82 00:05:55,592 --> 00:05:59,900 And now all of the areas that were being shown in the scroll bar are gone. 83 00:05:59,900 --> 00:06:03,510 Let's run the app and verify that everything is still working. 84 00:06:03,510 --> 00:06:06,410 If you need to, right-click on the console app project and 85 00:06:06,410 --> 00:06:09,020 select Set as StartUp Project. 86 00:06:09,020 --> 00:06:11,100 So that Visual Studio will build and 87 00:06:11,100 --> 00:06:15,050 start that project instead of the Class Library project. 88 00:06:15,050 --> 00:06:18,320 Visual Studio will bold the name of the startup project, so 89 00:06:18,320 --> 00:06:21,080 that it's easy to identify in the Solution Explorer. 90 00:06:22,660 --> 00:06:27,200 And, we got an error, System.NotSupportedException. 91 00:06:27,200 --> 00:06:30,420 Model compatibility cannot be checked because the database does 92 00:06:30,420 --> 00:06:32,770 not contain model metadata. 93 00:06:32,770 --> 00:06:34,800 Model compatibility can only be checked for 94 00:06:34,800 --> 00:06:39,430 databases created using Code First or Code First Migrations. 95 00:06:39,430 --> 00:06:42,750 This is an odd error, at the beginning of this video, 96 00:06:42,750 --> 00:06:46,300 we were able to run the console app, so what happened? 97 00:06:46,300 --> 00:06:49,360 Let's ensure that our database actually exists. 98 00:06:49,360 --> 00:06:52,303 Open the SQL Server Object Explorer window, and 99 00:06:52,303 --> 00:06:54,543 drill down to the list of databases. 100 00:06:57,600 --> 00:06:59,019 Here's our database. 101 00:07:00,945 --> 00:07:03,020 And a list of it's tables. 102 00:07:03,020 --> 00:07:06,655 Why would EF think that our database wasn't created using the Code First 103 00:07:06,655 --> 00:07:07,820 workflow? 104 00:07:07,820 --> 00:07:10,670 Let's ask ourselves what does EF do the first 105 00:07:10,670 --> 00:07:15,600 time that a DB set property is accessed on the database context? 106 00:07:15,600 --> 00:07:18,880 If you're following along, go ahead and pause the video, and 107 00:07:18,880 --> 00:07:21,780 see if you can troubleshoot this error on your own. 108 00:07:21,780 --> 00:07:24,570 When you're ready to learn the solution, unpause the video. 109 00:07:26,890 --> 00:07:30,000 How did you do, were you able to resolve the error? 110 00:07:30,000 --> 00:07:32,530 The first time that a DB set property is accessed 111 00:07:32,530 --> 00:07:37,840 on the database context EF will compare the in memory representation of our model 112 00:07:37,840 --> 00:07:41,470 with the model queried from the MigrationHistory table. 113 00:07:41,470 --> 00:07:45,010 An application can have more than one database context. 114 00:07:45,010 --> 00:07:48,840 So in order for EF to retrieve the model for the correct context, 115 00:07:48,840 --> 00:07:53,250 they'll filter the table rows by the context key column value, 116 00:07:53,250 --> 00:07:57,310 which is the name of our context class type, including its name space. 117 00:07:57,310 --> 00:07:59,840 We can see that our context key column value 118 00:07:59,840 --> 00:08:04,220 is ComicBookLibraryManager .Data.Context. 119 00:08:04,220 --> 00:08:07,960 But we moved the context class into the Class Library. 120 00:08:07,960 --> 00:08:12,100 And changed the classes root name space to the ComicBookShared. 121 00:08:12,100 --> 00:08:17,135 Because of this change, EF wasn't able to find the model data for our context class. 122 00:08:17,135 --> 00:08:20,195 Let's update the context key column value to the correct value. 123 00:08:20,195 --> 00:08:26,759 ComicBookShared.Data.Context, close the tab and run the app again. 124 00:08:29,413 --> 00:08:31,800 And now the app runs as expected.