Adding the Class Library Project8:32 with James Churchill
Let's add a class library project to the existing ComicBookLibraryManager Visual Studio solution and move the Entity Framework related classes from the console app project to the class library project.
To follow along committing your changes to this course, you'll need to fork the dotnet-comic-book-library-manager repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-comic-book-library-manager git checkout tags/v1.3 -b adding-the-class-library-project
The project files for the Comic Book Library Manager console app are available at http://treehouse-project-downloads.s3.amazonaws.com/dotnet-ef-basics-comic-book-library-manager-completed.zip.
Visual Studio Community 2017
Visual Studio 2017 uses a brand new installer that is vastly superior to the previous version. It's now easier to just install the parts of Visual Studio that you actually need. For more information on how to install VS 2017 (including a download link), see https://docs.microsoft.com/en-us/visualstudio/install/install-visual-studio.
CTRL+H- Open the "Quick Replace" dialog
ALT+R- Replace next
ALT+A- Replace all
CTRL+SHIFT+S- Save all open files
SQL Server LocalDB Installation and Configuration
For more information on how to verify that SQL Server LocalDB is correctly installed and configured for use with Entity Framework, see this video in the Entity Framework Basics course.
.NET Standard and the .NET Standard Class Library Project Template
The .NET Standard is a formal specification of the .NET APIs. Over time, all of the available .NET runtimes (.NET Framework, .NET Core, Mono) will adhere to this specification so that code written to the .NET Standard will be portable across all of those runtimes.
For more information see https://docs.microsoft.com/en-us/dotnet/standard/library.
Package Management with NuGet
For more information on how to manage .NET packages using NuGet, see this Treehouse workshop.
In this video, we'll be completing the first part of the setup process, 0:00 adding the class library project. 0:04 I've already downloaded the ComicBookLibraryManager console app 0:06 project files and opened the solution in Visual Studio Community 2017. 0:09 If you're following along, see the teacher's notes for 0:14 a link to download the files, or for more information about Visual Studio. 0:17 Before we make any changes, 0:21 let's run the app to ensure that everything works as expected. 0:23 The first time that you run the app, EF will check the default SQL Server LocalDB 0:27 instance to see if the ComicBookLibraryManager database exists. 0:32 And if it doesn't exist, EF will create and see the database. 0:36 If you run into any database related issues, see the teacher's notes for 0:41 resources that will help you verify that SQL Server LocalDB is installed and 0:44 configured correctly for use with EF. 0:49 Now that we've verified that the console app is working correctly, 0:52 let's add the class library project. 0:55 In this Solution Explorer window, notice that the ComicBookLibraryManager project 0:58 is located underneath the Visual Studio Solution named, ComicBookLibraryManager. 1:03 A solution can contain one or more projects, to add a new project 1:08 right click on the solution and select Add>New project. 1:13 If you select the Visual C# category on the left, you'll see that there's 1:20 more than one project type that contains the name Class Library. 1:24 For our solution, 1:29 we want the project type that is named Class library (.NET Framework). 1:30 For more information about the .NET standard and 1:35 the .NET standard Class Library project type, see the teacher's notes. 1:37 Name the project ComicBookShared. 1:42 Verify that the selected .NET Framework version is set 1:46 to .NET Framework 4.6.1, then click the OK button. 1:50 The project template adds a file in the root of the project name Class1.cs. 1:55 Go ahead and delete that file. 2:01 If we open the packages.config file in the root of the console app project, 2:05 we can see that the project already has a reference to 2:10 the EntityFramework NuGet package. 2:13 Since we're moving our EF related classes to the Class Library project, 2:16 it also needs to have reference to the EntityFramework NuGet package. 2:20 To do that, right-click on the class library project in Solution Explorer, and 2:24 select Manage NuGet Packages. 2:29 Select the Browse tab, if it's not already selected, and 2:32 install the EntityFramework package 2:35 Go ahead and close all of the open tabs once the package is finished installing. 2:46 Now we're ready to move the EF related classes to the Class Library project. 2:51 The Data folder contains the Context, DatabaseInitializer and 2:56 Repository classes, while the Models folder contains all of our indie classes. 3:00 Visual Studio makes copying folders between projects easy to do. 3:06 Simply drag and drop the Data and 3:10 Models folders from the console app project to the Class Library project. 3:12 Notice that I'm dragging the folders onto the name of the project, 3:16 in order to copy the folders into the root of the project. 3:20 We no longer need the Data and 3:23 Models folder in the console app project, so go ahead and delete them. 3:25 If we open the Context.cs file, we'll notice that the name space still 3:31 reflects the file's original project: ComicBookLibraryManager. 3:35 Typically the first part of the name space matches the name of the project that 3:39 the files are contained within. 3:43 This isn't a hard requirement, we could leave the name space as it is. 3:45 But I like to keep my code clean, and it's simple enough to do, so let's fix it. 3:50 Select the text ComicBookLibraryManager and 3:55 press Ctrl+H to open the Quick Replace dialog. 3:58 Set the replacement term to ComicBookShared and 4:02 the scope of the replacement to the Current Project. 4:05 Then press Alt+R to replace references one at a time or 4:07 Alt+A to replace them all at once, which is what I'll do. 4:12 Press Ctrl+Shift+S to save all the open files. 4:19 Now, let's update the console app project, 4:22 first we need to add a reference to the Class Library project. 4:25 Right-click on References and select Add Reference. 4:29 On the left, make sure that projects is selected, then select the ComicBookShared 4:33 project and click OK. 4:38 In the Program.cs file, 4:45 we can see here in the scroll bar there are numerous compilation errors. 4:47 That's what these red areas represent, we can right-click on the scroll bar, select 4:51 Scroll Bar Options and set the vertical scroll bar behavior to use map mode. 4:56 Now we can just hover over one of the red areas to see a pop-up preview of the code 5:01 in that part of the file Artist, Role has squiggles, ComicBook has red squiggles. 5:06 Hm, looks like the compiler is having trouble locating 5:15 all of the types that we moved to the Class Library project. 5:19 Let's take a look at our using statements. 5:22 Need to update a couple of using statements here at the top of the file. 5:28 Using ComicBookLibraryManager.Data, and using ComicBookLibraryManager.Models. 5:32 Both Data and Models are now located in the ComicBookShared project. 5:39 And now all of the areas that were being shown in the scroll bar are gone. 5:55 Let's run the app and verify that everything is still working. 5:59 If you need to, right-click on the console app project and 6:03 select Set as StartUp Project. 6:06 So that Visual Studio will build and 6:09 start that project instead of the Class Library project. 6:11 Visual Studio will bold the name of the startup project, so 6:15 that it's easy to identify in the Solution Explorer. 6:18 And, we got an error, System.NotSupportedException. 6:22 Model compatibility cannot be checked because the database does 6:27 not contain model metadata. 6:30 Model compatibility can only be checked for 6:32 databases created using Code First or Code First Migrations. 6:34 This is an odd error, at the beginning of this video, 6:39 we were able to run the console app, so what happened? 6:42 Let's ensure that our database actually exists. 6:46 Open the SQL Server Object Explorer window, and 6:49 drill down to the list of databases. 6:52 Here's our database. 6:57 And a list of it's tables. 7:00 Why would EF think that our database wasn't created using the Code First 7:03 workflow? 7:06 Let's ask ourselves what does EF do the first 7:07 time that a DB set property is accessed on the database context? 7:10 If you're following along, go ahead and pause the video, and 7:15 see if you can troubleshoot this error on your own. 7:18 When you're ready to learn the solution, unpause the video. 7:21 How did you do, were you able to resolve the error? 7:26 The first time that a DB set property is accessed 7:30 on the database context EF will compare the in memory representation of our model 7:32 with the model queried from the MigrationHistory table. 7:37 An application can have more than one database context. 7:41 So in order for EF to retrieve the model for the correct context, 7:45 they'll filter the table rows by the context key column value, 7:48 which is the name of our context class type, including its name space. 7:53 We can see that our context key column value 7:57 is ComicBookLibraryManager .Data.Context. 7:59 But we moved the context class into the Class Library. 8:04 And changed the classes root name space to the ComicBookShared. 8:07 Because of this change, EF wasn't able to find the model data for our context class. 8:12 Let's update the context key column value to the correct value. 8:17 ComicBookShared.Data.Context, close the tab and run the app again. 8:20 And now the app runs as expected. 8:29
You need to sign up for Treehouse in order to download course files.Sign up