Creating A Connection String and Working With SQL Server LocalDB - Microsoft Docs
Creating A Connection String and Working With SQL Server LocalDB - Microsoft Docs
In this article
Creating a Connection String and Working with SQL Server LocalDB
SQL Server Express LocalDB
by Rick Anderson
7 Note
An updated version of this tutorial is available here using the latest version of
Visual Studio . The new tutorial uses ASP.NET Core MVC, which provides many
improvements over this tutorial.
This tutorial teaches ASP.NET Core MVC with controllers and views. Razor Pages is
a new alternative in ASP.NET Core, a page-based programming model that makes
building web UI easier and more productive. We recommend you try the Razor
Pages tutorial before the MVC version. The Razor Pages tutorial:
Is easier to follow.
Covers more features.
Is the preferred approach for new app development.
LocalDB is a lightweight version of the SQL Server Express Database Engine that starts
on demand and runs in user mode. LocalDB runs in a special execution mode of SQL
Server Express that enables you to work with databases as .mdf files. Typically, LocalDB
database files are kept in the App_Data folder of a web project.
SQL Server Express is not recommended for use in production web applications.
LocalDB in particular should not be used for production with a web application because
it is not designed to work with IIS. However, a LocalDB database can be easily migrated
to SQL Server or SQL Azure.
By default, the Entity Framework looks for a connection string named the same as the
object context class ( MovieDBContext for this project). For more information see SQL
Server Connection Strings for ASP.NET Web Applications .
Open the application root Web.config file shown below. (Not the Web.config file in the
Views folder.)
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string 2/4
5/10/21 14:53 Creating a Connection String and Working with SQL Server LocalDB | Microsoft Docs
XML = Copy
<add name="MovieDBContext"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial
Catalog=aspnet-MvcMovie;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf"
providerName="System.Data.SqlClient"
/>
The following example shows a portion of the Web.config file with the new connection
string added:
XML = Copy
<connectionStrings>
</connectionStrings>
The two connection strings are very similar. The first connection string is named
DefaultConnection and is used for the membership database to control who can access
the application. The connection string you've added specifies a LocalDB database
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string 3/4
5/10/21 14:53 Creating a Connection String and Working with SQL Server LocalDB | Microsoft Docs
named Movie.mdf located in the App_Data folder. We won't use the membership
database in this tutorial, for more information on membership, authentication and
security, see my tutorial Create an ASP.NET MVC app with auth and SQL DB and deploy
to Azure App Service.
The name of the connection string must match the name of the DbContext class.
C# = Copy
using System;
using System.Data.Entity;
namespace MvcMovie.Models
{
}
}
You don't actually need to add the MovieDBContext connection string. If you don't
specify a connection string, Entity Framework will create a LocalDB database in the users
directory with the fully qualified name of the DbContext class (in this case
MvcMovie.Models.MovieDBContext ). You can name the database anything you like, as long
as it has the .MDF suffix. For example, we could name the database MyFilms.mdf.
Next, you'll build a new MoviesController class that you can use to display the movie
data and allow users to create new movie listings.
Previous
Next
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string 4/4