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?
-
1That 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 asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/…. 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.– Guillermo Oramas R.2013-11-26 13:42:02 +00:00Commented Nov 26, 2013 at 13:42
-
@Guillelon Thank you :) I was learning MVC, so I'll switch to MVC 5 to continue learning :)Zeeshan– Zeeshan2013-11-26 14:29:26 +00:00Commented Nov 26, 2013 at 14:29
2 Answers
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>
Comments
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.