3.3: Database and Connection String
In this module we'll talk about connection strings and complete our first migration for database set up.
Purpose
A connection string is a piece of code that helps us establish our initial connection to the database. The connection string includes the source database name and other parameters needed to establish the initial connection. For the next few steps we'll examine this important concept and get our database established.
Connection String
First, let us show you where the connection string is established:
Go to the ElevenNote.WebMVC assembly, and double click on the
Web.Config
file.Look at the
<connectionStrings>
on line 13.Notice the the name is
"DefaultConnection"
Go to ElevenNote.Data ->
IdentityModels.cs
In the
ApplicationDbContext
method, one of the arguments is"DefaultConnection"
This is how the application can communicate with the Web.Config file and persist application data.
Go back to ElevenNote.WebMVC ->
Web.Config
In the connection string, the name of the database is in two places.
Take the following connection string code and paste it over the top of your existing connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ElevenNote.mdf;Initial Catalog=ElevenNote;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Run the application
Again, the connection string helps us establish an initial connection to the database, so when we run the application, we will have our initial database created: 1. Run the application and register an account. 2. Make sure to remember the login/password you use to test with later. 3. In the top right of the application, you should be greeted with the email you entered
Database
Let's show you how to view the data in the SQL Server Object Explorer.
In the Quick Launch (top right), type in 'SQL'
Find the SQL Server Object Explorer (you can also get this by going to View -> SQL Server Object Explorer)
Find your ElevenNote database and expand the Tables folder
Right click on
dbo.ApplicationUser
and select View DataCheckout the data
Close the data tab
Right click on
dbo.ApplicationUser
and select PropertiesTake a look at the table's properties
Last updated