MVC3:unable to retrieve metadata for my model clas

2019-01-23 14:51发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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" />


回答4:

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.



回答5:

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