Changing the Database Name7:50 with James Churchill
Let's look at the options for changing the name of the generated database.
To follow along commiting your changes to this course, you'll need to fork the dotnet-comic-book-gallery-model repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-comic-book-gallery-model git checkout tags/v2.3 -b changing-the-database-name
Here's the database connection string to connect to the default SQL Server LocalDB instance and set the generated database name to "ComicBookGallery".
Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ComicBookGallery;Integrated Security=True;MultipleActiveResultSets=True
Before we look at the options for 0:00 changing the database name, let's delete our current database. 0:01 Deleting a database is not an operation that you'll do very often, 0:05 especially given that deleting a database 0:09 means that you'll forever lose access to the data contained within it. 0:12 Since our database is being generated by our app, and 0:16 it doesn't contain any data that we need to keep, it's safe to do. 0:20 We can delete our database by right-clicking here, 0:24 on the database in the SQL Server Object Explorer, and selecting Delete. 0:27 We'll be asked if we also want to delete the backup and 0:32 restore history information, and close any existing connections. 0:35 Be sure to check both options, that'll ensure that the delete operation will 0:40 succeed, even if we currently have an open connection to the database. 0:44 We're going to look at three options for 0:49 changing the name of the generated database. 0:51 The first and easiest of these options is to add a constructor to our context class. 0:54 And call the base class constructor that accepts a parameter for 0:59 the database name or connection string. 1:03 Public Context() then colon followed by the base keyword, 1:11 which allows us to call the members in the base class. 1:16 In this case, the base class constructor. 1:21 We'll take a look at how to use the connection string option in just a moment. 1:24 For now I'll just pass in the name of the database that 1:28 I'd like to use, ComicBookGallery. 1:33 Now let's run the app Press Enter to continue execution. 1:37 Then refresh the list of databases in the SQL Server Object Explorer window, 1:47 and we'll see our new ComicBookGallery database. 1:52 The DbContext base class constructor that we're calling 1:56 also accepts the database connection string. 1:59 This is our second option for changing the name of the database. 2:01 Here in Notepad, I have an example of a SQL Server database connection string. 2:05 The Data Source value is the name of the database server that we want to connect 2:10 to, which is a localdb instance named MSSQLLocalDB. 2:14 If we wanted to use a different database server other than localdb, or 2:19 a differently named instance, we could change this value. 2:24 The Initial Catalog value is the name of the database. 2:28 The Integrated Security value indicates that we want to use 2:32 Windows authentication. 2:35 And the MultipleActiveResultSets value indicates that we want to be able to run 2:38 more than one SQL batch on an open connection at the same time. 2:43 Setting this value to true, generally speaking, 2:47 helps to improve the overall performance of retrieving data from the database. 2:50 For more information about multiple active result sets, or 2:55 MARS, see the teacher's notes. 2:59 I'll copy and paste the connection string to the base class constructor method call. 3:02 Because our connection string contains a backslash, which is used to escape special 3:15 characters and text, we need to add another backslash before our backslash. 3:20 This tells the compiler that we actually want a backslash character, or 3:25 we can leave the single backslash and add an at sign before the starting quote. 3:30 This tells the compiler to treat our text as a verbatim string literal and 3:36 ignore any escape sequences contained within the text. 3:40 Let's also add the text ConnectionString as a suffix to the initial catalog name so 3:44 we don't have to delete our existing database. 3:49 Run the app again, and press Enter to continue execution. 4:00 Refresh the list of databases, and 4:06 we'll see our new ComicBookGalleryConnectionString database. 4:09 The third and last option for 4:13 changing the name of the database is to add a database connection string whose 4:15 name matches the name of our context class to our apps configuration file. 4:19 For console apps, we'll use the App.config file 4:24 in the root our project to add the necessary configuration. 4:28 For ASP.NET MVC apps, 4:32 you can use the web.config file in the root of the project. 4:34 We'll be covering how to use Entity Framework within MVC apps in another 4:37 course. 4:41 We start by adding a connectionStrings element right underneath 4:44 the root configuration element. 4:47 And within that element, we'll add an add element. 4:54 On the add element, 4:59 we'll add a name attribute whose value will match the name of our context class. 5:00 That's how EF will know to associate this connection string with our context. 5:06 Then we'll add a connection string attribute for our connection string, 5:11 which I'll cut and paste from the context class. 5:15 I'll also change the database name suffix to AppConfig. 5:35 Lastly, we'll add a providerName attribute to indicate which data provider to use. 5:52 Since we're using a SQL Server localdb database, 5:57 I'll set the value to System.Data.SqlClient. 6:02 Now that we have our connection string defined in the App.config file, 6:08 we can remove the constructor from the context class. 6:11 Run the app again, and press Enter to continue execution. 6:18 Refresh the list of databases, and 6:31 we'll see our new ComicBookGalleryAppConfig database. 6:33 Of the three options that we just saw, I like the third option, as it keeps 6:37 the configuration of our database location and name out of our code. 6:41 That makes it possible to change the location or 6:46 name of the database without having to rewrite our code. 6:49 Which is especially helpful when deployment applications 6:52 into other environments. 6:56 Let's remove the AppConfig suffix from the database name in the connectionString, so 6:59 our database name is just ComicBookGallery. 7:04 Then I'll do some cleanup and delete all of the existing databases. 7:13 Lastly, I'll run the app one more time, and 7:32 verify that our database is successfully created. 7:35 Refresh the list of databases, And here's our ComicBookGallery database. 7:39
You need to sign up for Treehouse in order to download course files.Sign up