Unable to retrieve metadata - MVC application

2019-01-27 00:06发布

I was following this to create a simple MVC application. After creating the model, when I tried to add controller, I got the following error :

Unable to retrieve metadata for "MvcApplication.Models.Movie". Invalid value for key "attachdbfilename".

enter image description here

Can someone tell why I am getting this error.

Update : I saw this wherein solution is provided by changing the providerName. But in my case it is already System.Data.SqlClient. Following are my connectionStrings :

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-MvcApplication-20130708120940;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
    <add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

3条回答
干净又极端
2楼-- · 2019-01-27 01:02

The error message is saying invalid value for AttachDbFilename which is part of your MovieDbContext connection string.

The connection string currently shows this AttachDbFilename=|DataDirectory|\Movies.mdf so it appears the mdf file is now missing from the App_Data folder.

查看更多
叛逆
3楼-- · 2019-01-27 01:08

I found the solution. the problem is the "|DataDirectory|". I Changed "|" to "\"

查看更多
再贱就再见
4楼-- · 2019-01-27 01:08

I just solved this problem. Even though I am new to MVC, I am bit embarrassed to say, I was trying a work around for another problem I was having and moved my SetInitializer into my classname : DbContext class.

The app worked just fine and the database was created fine as I changed the models but I couldn't add new controllers or modify current controllers using the scaffolding tool.

I began receiving errors. One of them is listed above and another is:
Unable to retrieve meta data for 'namespace.Models.class'. A file activation error occurred. The physical file name '\name.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

Moving the SetInitializer back into my Global.asax fixed the problem.

查看更多
登录 后发表回答