创建连接字符串并使用 SQL 服务器 LocalDB

Creating a Connection String and Working with SQL Server LocalDB

当我尝试从 visual studio 15 连接到 localdb 时遇到问题。 我已经安装了 SQL Server Express 2016

我正在学习本教程:http://www.asp.net/mvc/overview/getting-started/introduction/creating-a-connection-string

我创建模型和上下文 (MovieDBContext) class 并尝试设置连接:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="MovieDBContext"    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
/>

当我尝试访问显示所有电影的页面时:

 public ActionResult Index()
 {
     return View(db.Movies.ToList());
 }

我得到这个异常:

我尝试像这样编辑连接字符串,但也不起作用:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="MovieDBContext"   connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
/>  </connectionStrings>

我在使用 (local)MSSQLLocalDB

时得到了这个异常

我做错了什么?

MovieDBContext 连接字符串中缺少初始目录。 需要如下:

 <add name="MovieDBContext"   connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Initial Catalog=Movies;Integrated Security=True" providerName="System.Data.SqlClient"/>