1

当使用实体框架的CRUD集成添加控制器助手/棚架,我得到以下错误:添加控制器数据库错误在ASP.NET MVC 4

Unable to retrieve metadata for 'MyApp.Models.MyModel'. Cannot attach the file 
'C:\Users\Lev\AppData\Local\Temp\2d7daf19-d47f-df24-ac76-d3df4fg2sa\aspnet-MyApp- 
20130409170533.mdf' as database 'aspnet-MyApp-20130409170533.mdf'. 

的问题是:为什么找数据库在我的临时文件?该数据库文件位于:

C:\Projects\MyApp\MyApp\App_Data 

这是我的连接字符串:

Data Source=(LocalDb)\v11.0; 
Initial Catalog=aspnet-MyApp-20130409170533; 
Integrated Security=SSPI; 
AttachDBFilename=|DataDirectory|\aspnet-MyApp-20130409170533.mdf 

因此,这似乎是参考| DataDirectory目录|行为不端。我如何设置DataDirectory? 如果我手动将正确的路径放入AttachDBFilename属性,则脚手架起作用。但我仍然喜欢将它保留为| DataDirectory |。

+0

这解释了它很漂亮http://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented – 2013-04-10 02:02:29

+0

我不确定,但我相信web.config文件获取在任何其他代码之前执行/编译。因此,如果我在这样做的任何东西回答,它不会影响web.config连接字符串(这是脚手架控制器时使用的) – 2013-04-11 00:01:36

回答

1

只需重新启动Visual Studio。这有助于我的项目。