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

tereško
  • 58,060
  • 25
  • 98
  • 150
Hardworker
  • 229
  • 3
  • 8
  • 19
  • Probably because your connection string is wrong. Post it here. – James Hull Feb 20 '12 at 10:12
  • try this!!...helped me...hope it helps u as well... http://stackoverflow.com/questions/8238811/mvc-add-controller-is-unable-to-retrieve-metadata-config-system-failed-to-i Thanks!! – aromore Feb 23 '12 at 21:31
  • This is my connectionstring in web.config: – Hardworker Feb 29 '12 at 05:59
  • Check my answer here: [1] it should solve your problem [1]: http://stackoverflow.com/questions/6508516/asp-net-mvc-3-unable-to-find-the-requested-net-framework-data-provider/13401557#13401557 – Jim Nov 15 '12 at 16:12

5 Answers5

18

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

frennky
  • 12,581
  • 10
  • 47
  • 63
Sumit
  • 196
  • 1
  • 8
  • 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 02 '12 at 07: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.

Jon P
  • 826
  • 1
  • 7
  • 20
  • 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 '14 at 03:56
5

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" />
naim shaikh
  • 1,103
  • 2
  • 9
  • 20
  • 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 '12 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 '12 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? `` 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 '14 at 03: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.

Anders E. Andersen
  • 1,635
  • 2
  • 14
  • 20
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