8

I have problem when I try to connect to LocalDB from Visual Studio 15. I have installed SQL Server Express 2016

I'm following this tutorial: http://www.asp.net/mvc/overview/getting-started/introduction/creating-a-connection-string

I create a model and context (MovieDBContext) class and try to setup a connection:

<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"
/>

When I try to access the page where I show all movies:

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

I get this exception:

enter image description here

I tried to edit the connection strings like this, and this does not work either:

<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>

I get this exception when I use (local)MSSQLLocalDB

enter image description here

What am I doing wrong?

6
  • 3
    In Visual Studio, on the Tools Menu, can you connect to the database? Your database should be appearing in the Server Explorer, where you can find the correct connectionString. Commented Aug 4, 2016 at 12:28
  • 1
    What is the name of the class that inherits from DbContext?Your "db" class? Commented Aug 4, 2016 at 12:33
  • 2
    If you open Sql Server Management Studio can you login to (LocalDb)\v11.0 Commented Aug 4, 2016 at 12:38
  • 1
    if you have a db already (.mdf file) your AttachDbFileName should be pointing to the right path Commented Aug 4, 2016 at 12:45
  • 2
    @Forlani there's nothing wrong with his connection string.He's using EF code first and if the .mdf file doesn't exist it will be created. Commented Aug 4, 2016 at 12:48

1 Answer 1

20

The initial Catalog is missing in MovieDBContext connection string. It needs to be as follows:

 <add name="MovieDBContext"   connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Initial Catalog=Movies;Integrated Security=True" providerName="System.Data.SqlClient"/> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this work and .mdb is created inside App_Data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.