15

I am new to asp.net MVC3 platform i am following the tutorial which is posted on the asp.net website: http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part1-cs

But in the fifth step which i need to access my model class data from a controller i am facing some troubles, i have created model class and the context class but when i want to create a controller class using the scaffolding template(controller with read/write actions and views using entity framework) it gives me an error as below:

unable to retrieve metadata for 'full name of my modelclass'. Configuration system failed to initialize

4
  • Probably because your connection string is wrong. Post it here.
    – James Hull
    Feb 20, 2012 at 10:12
  • try this!!...helped me...hope it helps u as well... stackoverflow.com/questions/8238811/… Thanks!!
    – aromore
    Feb 23, 2012 at 21:31
  • This is my connectionstring in web.config: <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> <add name="MovieDBContext" connectionString="Data Source=|DataDirectory|MVCtest.sdf" providerName="System.Data.SqlServerCe.4.0"/> </connectionStrings>
    – Hardworker
    Feb 29, 2012 at 5:59
  • Check my answer here: [1] it should solve your problem [1]: stackoverflow.com/questions/6508516/…
    – Jim
    Nov 15, 2012 at 16:12

5 Answers 5

18

Just change your connection string provider type to System.Data.SqlClient

1
  • 2
    It does not work also I am not sure that It should work for .sdf. Adding the connection string as it is after adding the controller is a solution.
    – Sami
    Dec 2, 2012 at 7:15
12

There's no bug. It is just that there is a chronological order you must follow in order to reflect the model structure and be migrated to the database and have no errors:

  1. Create the controllers (do not include yet connection strings in Web.config or you would get the said error.)
  2. Add the connection string in Web.config with what's written in the tutorial.
  3. Finally, run the application.

NOTE: Do not manually create the compact database because Visual Studio will automatically create and map it for you along with the model structure as you run it and navigate to that controller in your browser. Just ensure that you have the correct path of the database in your Web.config. Hope this helps! Regards.

1
  • By following your tip, I don't see a database called Movies created under Server Explorer window of VStuio - although your tips work. I would like to see the db. Plus I am having full version of SQL Server.
    – bonCodigo
    Sep 30, 2014 at 3:56
5
+50

Try this one...

<add name="MovieDBContext" connectionString="data source=.\SQLEXPRESS;&#xD;&#xA;         Integrated Security=SSPI;&#xD;&#xA; AttachDBFilename=|DataDirectory|\MVCtest.sdf;&#xD;&#xA;         User Instance=true" providerName="System.Data.SqlClient" />
3
  • Yes, I can add to this as well sometimes it's just best to start from scratch. Remove any existing connection strings from the web.config, remove the Entity Model (.edmx), and then re-create it fresh. Sometimes I think something gets tweaked and messed everything up. Also, if using partial classes with the same name, make sure to select the proper entity class when scaffolding.
    – atconway
    May 11, 2012 at 17:24
  • 1
    +1 And if you have the full edition of SQL Server installed, point it at a database in localhost data source=localhost;Integrated Security=SSPI;Database=test;
    – Andomar
    Sep 22, 2012 at 15:07
  • @Andomar I am working with the full version of SQL Server. So there's no Movie DB existing in my server. I am following the same tutorial as per OP. And I too get the same error. What's going wrong with my connstring? <add name="MovieDBContext" connectionString="Data Source=(LOCAL);Initial Catalog=Movies;User ID=sa;Password=password" /> If I add providerName="System.Data.SqlClient", then it works - although it doesn't make sense to me. Yet I am unable to see the Movie db is created in Server Explorer of VS. So I am not sure if I am on the right direction.
    – bonCodigo
    Sep 30, 2014 at 3:58
5

I changed the providerName to System.Data.SqlClient like suggested in another anwer. This allowed me to create the controller in the add controller step. But the application wouldn't display the movies resource at all when I ran it. I then changed it back to System.Data.SqlServerCe.4.0 like in the tutorial and everything worked.

After reading other discussions around the web I have found another method.

If you do not add the connection string until after you have created the controller class, then it will work also. It kind of seems like a bug.

2
  • 2
    +1. Adding the connection string as it is after adding the controller is a solution.
    – Sami
    Dec 2, 2012 at 7:15
  • 1
    Same worked for me. I added the connection string after adding the controller.
    – maulik13
    Apr 4, 2013 at 8:05
0

Make sure you add the connection string in the correct web.config file because there's 2 (one at the solution level and another at the view level). You want to add it at the view level. Your crud stuff will get created just fine after you fix the connect string then create the controller..Thanks, Maurice Maglalang

Not the answer you're looking for? Browse other questions tagged or ask your own question.