Configuring Entity Framework5:03 with James Churchill
Before we start updating the controller, two things are needed in order to ensure that EF is properly configured for our web app—configure the server and database that EF should use and configure the database initializer. We'll start with configuring the server and database.
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.5 -b configuring-entity-framework
Entity Framework Config File Settings
In general, Entity Framework follows a "convention over configuration" principle. But sometimes you'll find that you need to explicitly change a setting. When you do, EF conveniently allows you to set a number of settings via your application's configuration file. For more information see https://msdn.microsoft.com/en-us/data/jj556606.aspx.
Customizing Web App Configuration per Environment
For information on how to customize configuration per environment, see the following resources.
We need to do two things in order to ensure that EF is properly configured for 0:00 our web app. 0:04 Configure the server and 0:05 database that EF should use and configure the database initializer. 0:06 Can you recall the method that allows you to specify the server and 0:12 database via configuration to do that? 0:16 We need to provide a database connection string via our apps configuration file. 0:19 In order to ensure that both the console and web apps use the same server and 0:24 database, we can simply reuse the database connection string 0:28 in the console apps app config file. 0:32 Select and copy the entire connection strings element and 0:35 it's single child element to the clipboard. 0:38 Then, open the web config file in the root of the web app project. 0:41 Be careful to not open the web config file located in the views folder. 0:47 The web config file located in the views folder is specifically for 0:52 configuring the MVC razor view engine. 0:56 Where as the web config file located in the root of the web app project is for 0:58 configuring the overall NVC application. 1:03 Scroll down to the bottom of the file and 1:06 paste the contents of the clipboard just before the closing configuration element. 1:08 Remember, adding a database connection string 1:13 with a name that matches your context class name 1:16 will cause EF to implicitly use it when attempting to connect to the database. 1:19 Next we need to configure the database initializer. 1:23 Let's start by reviewing how the console app was handling this. 1:27 If we take a look at the constructor for the context class 1:30 that we moved out of the console app project to the class library project, 1:34 we can see that it's making a call to the database set initializer method. 1:39 In order to configure EF to use our custom database initializer class, 1:43 databaseInitializer, we're calling this setInitializer method 1:48 every time that the contents class constructor is called. 1:52 It's actually only necessary to call the setInitializer method once 1:56 per application start before the first use of the context. 2:00 ASP.NET actually makes this very easy to do. 2:04 The global asax cs file defines a class containing a method 2:09 named Application_Start, which is called every time that the web app is started. 2:14 So, we could just cut and paste the code from the content class constructor 2:20 to this method, but EF gives us a better option. 2:24 We can configure the database initializer using our app's configuration file. 2:29 This gives us a way to bury the database initializer per environment 2:33 by customizing the configuration file for that environment. 2:38 The specifics of how you go about customizing your Web Apps configuration 2:42 per environment will vary depending on your specific environments and 2:46 how you're doing your deployments. 2:51 See the teachers notes for more information. 2:53 Let's update the Web.config file to configure the database initializer. 2:55 Scroll to the entityFramework element and add a context child element. 3:01 I'm going to add the new element just above the defaultConnectionFactory 3:11 element. 3:15 But the order of the child elements isn't specific. 3:16 Then, add a context child element to the context element. 3:19 We need to identify which context we want to configure. 3:23 We do that by adding a type attribute. 3:27 The value of the type attribute needs to be an assembly qualified type name which 3:39 is the name of the type with its name space, followed by a comma, and 3:43 the name of the assembly that contains the type. 3:47 If we had more than one context type that we needed to configure, 3:51 we could add a context element per a context type. 3:54 To configure the database initializer for our context, we add a databaseInitializer 3:57 child element to the context element with its type attribute set to our custom 4:03 database initializer type, ComicBookShared.Data.DatabaseInitializer, 4:08 ComicBookShared. 4:18 Now, we can remove the call to the Database.SetInitializer method in 4:21 the context constructor. 4:25 Actually, since it's the only call that we're making in the constructor, 4:30 we can remove the entire constructor. 4:34 Lastly, let's update the console lab project's app config file so 4:36 the console app will still work as expected. 4:40 First, let's copy the context element from the web app's web config file and 4:44 open the app config file, scroll to the entityFramework element. 4:49 And paste the context element just above the default connection factory element. 4:56
You need to sign up for Treehouse in order to download course files.Sign up