MVC5 Web应用连接SQL Server的ConnectionString属性及配置问题
Hey there! Let's tackle your MVC5 database connection issue first, then break down every part of that connection string so you know exactly what each piece does.
First, Fix the "Data Source Not Found" Error
Your error is almost certainly due to an outdated LocalDB instance name. (LocalDB)\v11.0 is the instance name for SQL Server 2012's LocalDB. If you've installed a newer version of SQL Server (2016 or later), the default auto-updating LocalDB instance is (LocalDB)\MSSQLLocalDB. Swap that into your connection string first—this should resolve the "data source not found" problem right away.
If you're unsure which instance you have, open Command Prompt and run sqllocaldb info to see all installed LocalDB instances, then use the active one.
Let's walk through every part of your <connectionStrings> configuration, from the parent node down to each individual parameter.
1. The <connectionStrings> Node
This is a dedicated section in Web.config for storing all your database connection configurations. Each <add> tag inside it represents a single database connection setup.
2. The <add> Tag's Core Attributes
name: This is the unique identifier for your connection string. Your MVCDbContextclass (likeMovieDBContext) will use this name to locate the correct connection settings. Double-check that the name in your code matches this exactly—typos here are a common gotcha.providerName: Specifies the .NET database driver to use.System.Data.SqlClientis the official driver for SQL Server, so this stays the same unless you're working with a different database (like MySQL or PostgreSQL).
3. The connectionString Parameter Details
Your connection string is split into semicolon-separated key-value pairs. Here's what each one means:
Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\Movies.mdf; Integrated Security=True
Data Source: Defines the database server or instance to connect to. For LocalDB (SQL Server's lightweight dev-focused version), this is the instance name. For full SQL Server, this would be something likelocalhost,.\SQLEXPRESS, or a remote server's IP/hostname.AttachDbFilename: Points to the physical SQL Server database file (.mdf) you want to attach. The|DataDirectory|placeholder automatically maps to your ASP.NET project'sApp_Datafolder—this is the standard location for storing data files in web apps. So|DataDirectory|\Movies.mdftranslates toApp_Data\Movies.mdf.Integrated Security=True: Enables Windows Integrated Authentication, meaning your app uses the current Windows user's credentials to connect to the database. No username/password is needed here. If you set this toFalse, you'd need to addUser IDandPasswordparameters to specify a SQL Server account.
Quick Extra Tip
Make sure your project has an App_Data folder—if it doesn't, right-click your project in Visual Studio → Add → New Folder, name it App_Data, and either place your Movies.mdf file there or let Entity Framework auto-generate it when you run the app.
内容的提问来源于stack exchange,提问作者Hojat Moshkabadian

