4

I'm learning MVC. I just need to clear something. I'm following THIS tutorial. This guy uses LocalDB to store Movie object. He just adds a connection string and then after adding controller, as described in tutorial, CRUD action methods are automatically added. that guy used sentence something like, "that's all you have to do to store your movie object in your localdb". Database is automatically created (code first approach). (I know how to work with Entity Framework, where we have to create some model to map our database). But this tutorial is confusing me. There's nothing mentioned about creating database etc. while his connection string contains a word, "Movie.mdf" (under Data Source). Finally, following him, I'm getting server not found error (26). Am i missing something, as new to MVC?

Zeeshan
  • 2,884
  • 3
  • 28
  • 47
  • 1
    That tutorial is great but it is kind of confusing when he talks about the conectionString. To create the DataBase I recommend you EF Code-First migration http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application. I know is intended for MVC-5 but it will help you. You'll learn for sure how to create the DB, everything else related with MVC is the same from the other tutorial. Let me know. – Guillermo Oramas R. Nov 26 '13 at 13:42
  • @Guillelon Thank you :) I was learning MVC, so I'll switch to MVC 5 to continue learning :) – Zeeshan Nov 26 '13 at 14:29

2 Answers2

8

Got the solution. The problem actually was that I was not calling the base constructor for dbContext class. Because I was assuming that keeping the name of the connection string and my class same will be enough. But it didn't work, and no db was created. I changed it as following piece of code, and it worked.

public class myDbClass : DbContext
{
  public myDbClass()
    : base("myConString")
  {
     //logic;
  }
}

connection string is first placed with the SAME name in web.config, so that it can look up for.

<connectionStrings>
    <add name="myConString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/> 
</connectionStrings>
logical8
  • 1,092
  • 9
  • 12
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
1

Is Local Db installed on your machine? It's installed when you install MS SQL server, but I think you have a checkmark, it's not installed by default.

When you install EntityFramework from NuGet, localdb is the default in your web.config, so it's true there is normally nothing else to do, even if it might not be the best way.

BernardG
  • 1,956
  • 3
  • 17
  • 25
  • How can i check if it's installed or not? I do not have external MS SQL manager, but i guess, i have it's express edition, which is by default integrated with MS Visual Studio 2010. (I can create database in server explorer). – Zeeshan Nov 26 '13 at 14:33
  • My understanding is that installing LocalDB come with SQL Server Express 2012, so I don't think you have it with VS 2010. This page has a link to download it by itself: http://www.mssqltips.com/sqlservertip/2694/getting-started-with-sql-server-2012-express-localdb/ If it's not installed, it might explain your error. There is a awnser here http://stackoverflow.com/questions/11628316/sql2012-localdb-how-to-check-in-c-sharp-if-it-is-currently-installed allowing you to see if it's installed or not. – BernardG Nov 27 '13 at 15:00