Excluding Test Data in Production5:54 with James Churchill
Let's see how we can update our Configuration class to exclude our test data when we're not building a debug version of our application.
To follow along commiting your changes to this course, you'll need to fork the dotnet-ef-migrations repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-ef-migrations git checkout tags/v1.8 -b excluding-test-data-in-production
Creating Custom Build Configurations
For a walkthrough of how to create a custom build configuration, see the following on MSDN.
Once you've created your custom build configuration, you can then define a symbol that can be used with preprocessor directives. See this MSDN resource for information on how to define symbols (be sure to scroll down to the section labeled "To set this compiler option in the Visual Studio development environment").
Here's an article on CodeProject that walks through a complete example of how to use conditional compilation to control runtime settings for different deployment scenarios.
- Visual Studio: Use Conditional Compilation to Control Runtime Settings for Different Deployment Scenarios
For more information about C# preprocessor directives, see the following resources.
For more information about Debug and Release configurations, see the following on MSDN.
It's not unusual for the needs and requirements for sea dated to be different 0:00 between the various environments that you need to support. 0:04 For instance a mike look at data, test data, 0:07 is often only needed in local developer and test environments. 0:10 And once you're ready to release a version of your application into production, 0:15 you'll want to exclude it. 0:19 Up until now, we've been using the debug built configuration by default 0:21 Visual Studio projects also include a release configuration. 0:26 As the name suggest, the debug configuration is used for 0:30 debugging your application. 0:34 While the release configuration is used to build a version of your application 0:36 that's optimized for release into your production environment. 0:40 It's also possible to create custom build configurations. 0:44 For example, you could create a test build configuration to use 0:48 when building your application for your test environment. 0:52 See the teachers notes for 0:55 more information on how to create custom build configurations. 0:56 Preprocessor directives can be used to conditionally include or 1:00 exclude code based on build configurations. 1:05 Preprocessor directives are instructions for 1:08 the compiler to preprocess before it compiles your code. 1:11 Lets see how we could update our configuration class to exclude our test 1:15 data when we're not building a debug version of our application. 1:19 To do that, we can wrap our test data in an if, preprocessor directive. 1:23 In order to only include that code in the compilation 1:28 of our application when the debug symbol is defined. 1:31 I'm going to consider everything other than the row look up data to be test data. 1:36 So, a place to preprocessor directive on a new line before our artist ID Constance. 1:41 Preprocessor directives start with a pound sign, 1:47 followed by the name of the directive. 1:50 Then, we type the name of the symbol that we want to check for. 1:53 You can think of preprocessor symbols as variables that can be used 1:58 with preprocessor directives. 2:02 The DEBUG symbol is defined by the DEBUG build configuration but 2:05 not by the release build configuration. 2:10 So that's a good symbol for 2:12 us to use in order to determine which build configuration is selected. 2:13 Symbols or case sensitive and then DEBUG symbol is defined using uppercase letters. 2:18 So it's important to type debug in all uppercase letters and not Debug or debug. 2:24 Otherwise the #if directive won't ever evaluate to true. 2:32 Preprocessor directives aren't code statements. 2:41 So they don't end with a semicolon. 2:43 Now let's scroll down to the bottom of our test data and add the endif directive. 2:46 #endif Visual Studio will 2:57 give us a subtle visual indication when code isn't being included in compilation. 3:07 If we switch to the release build configuration, we can see that the editor 3:13 will dim the color of the code in between our preprocessor directives to gray. 3:16 If we switch back to debug build configuration, 3:22 we can see that the default syntax color highlighting returns, 3:25 indicating that the code will be included in the compilation of our application. 3:29 Let's test our changes. 3:33 Switch to the release build configuration. 3:35 Delete or drop our database 3:40 Run the update database command. 3:52 Update- database. 3:58 And then, run the application without debugging by pressing Ctrl+F5. 4:04 And now, we're not getting any comic books returned from 4:08 our query press Enter twice to continue execution. 4:13 Let's double check that the comic book table doesn't contain any data. 4:20 And the table doesn't contain any rows just as we expected. 4:34 If we switch to the debug build configuration Run 4:41 the update database command, 4:45 And press F5 to run our application, 4:56 we'll see our list of comic books from our test data. 4:59 We're just scratching the surface of what's possible with build configurations 5:05 and preprocessor directives. 5:09 See the teacher's notes for information on both. 5:11 Let's do a quick review of the section. 5:14 We saw how to enable migrations for 5:17 our project using the Enable migrations command. 5:19 And our initial migration using the add migration command, and 5:22 update our database using the update database command. 5:27 We also saw how to use the Code First migrations 5:30 configuration class to seed our database with look up and test dat. 5:34 In the next section, we'll dig deeper into Code First migrations Well, 5:37 out of migration for a simple model change. 5:43 Look at an example of how to modify a migration, 5:45 see how to downgrade our database and review our deployment options. 5:48 See you after the break. 5:53
You need to sign up for Treehouse in order to download course files.Sign up