asp.net-mvc MVC3:当我想从控制器访问模型数据时,无法检索模型类的元数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9359108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC3:unable to retrieve metadata for my model class when i want to accesse model data from controller
提问by Hardworker
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
我是 asp.net MVC3 平台的新手,我正在学习发布在 asp.net 网站上的教程:http: //www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-开始与 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
无法检索“我的模型类的全名”的元数据。配置系统初始化失败
回答by Sumit
Just change your connection string provider type to System.Data.SqlClient
只需将您的连接字符串提供程序类型更改为 System.Data.SqlClient
回答by Jon P
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:
没有错误。只是有一个必须遵循的时间顺序才能反映模型结构并迁移到数据库并且没有错误:
- Create the controllers (do not include yet connection strings in Web.configor you would get the said error.)
- Add the connection string in Web.configwith what's written in the tutorial.
- Finally, run the application.
- 创建控制器(不要在Web.config 中包含连接字符串,否则会出现上述错误。)
- 使用教程中编写的内容在Web.config 中添加连接字符串。
- 最后,运行应用程序。
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.
注意:不要手动创建紧凑型数据库,因为 Visual Studio 会在您运行它并在浏览器中导航到该控制器时自动为您创建和映射它以及模型结构。只需确保您的Web.config 中有正确的数据库路径。希望这可以帮助!问候。
回答by naim shaikh
Try this one...
试试这个...
<add name="MovieDBContext" connectionString="data source=.\SQLEXPRESS;
 Integrated Security=SSPI;
 AttachDBFilename=|DataDirectory|\MVCtest.sdf;
 User Instance=true" providerName="System.Data.SqlClient" />
回答by Anders E. Andersen
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.
我将 providerName 更改为 System.Data.SqlClient 就像在另一个anwer中建议的那样。这允许我在添加控制器步骤中创建控制器。但是当我运行该应用程序时,它根本不会显示电影资源。然后我把它改回 System.Data.SqlServerCe.4.0 就像在教程中一样,一切正常。
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.
如果您在创建控制器类之后才添加连接字符串,那么它也将起作用。这有点像一个错误。
回答by Maurice Maglalang
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
确保在正确的 web.config 文件中添加连接字符串,因为有 2 个(一个在解决方案级别,另一个在视图级别)。您想在视图级别添加它。在您修复连接字符串然后创建控制器后,您的 crud 内容将被创建得很好..谢谢,莫里斯·马格拉朗

