Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Local DB in MVC 4 application

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?

like image 241
Zeeshan Avatar asked Jan 12 '23 19:01

Zeeshan


1 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>
like image 149
2 revs, 2 users 91% Avatar answered Mar 11 '23 08:03

2 revs, 2 users 91%