10

From scratch, I made a new solution with two projects: one was MVC 3 and the other a supporting EF 4.2 project. The whole thing builds successfully. From the MVC project I open the "Add Controller" dialogue and have it generate code based on the context and model I select from the supporting EF project. The "add controller" dialogue fails with the message:

Unable to retrieve metadata for 'MyModelClass'. Configuration system failed to initialize.

I've noticed that the "add controller" dialogue is actually attempting to fetch the database connection string from its web.config file. First, this strikes me as goofy-ish, since the supporting EF project already has an app.config with the connection string. But never-minding that, the best I can figure is that the connection string in the web.config is bad somehow. This is what it looks like:

<add name="Monsters2Entities" 

    connectionString="
      metadata=res://*/Monsters.csdl|
               res://*/Monsters.ssdl|
               res://*/Monsters.msl;
      provider=System.Data.SqlClient;
      provider connection string=&quot;
        data source=.;
        initial catalog=Monsters2;
        integrated security=True;
        pooling=False;
        multipleactiveresultsets=True;
        App=EntityFramework
      &quot;" 
      providerName="System.Data.EntityClient" 
/>

The connection string doesn't actually have all the ridiculous line breaks and indentation - I'm just trying to make it easier to read. Anyway, that connection string is basically identical to the connection string used in the supporting EF project upon which it is modelled. How do I correct the situation, to make the "add controller" dialgoue happy?

Brent Arias
  • 29,277
  • 40
  • 133
  • 234
  • I had a web.config file for one of my projects and it was divided into parts. When I try to add controller like you did, I got the same error. I make it one file and it worked magically. didn't dig into it much though. – tugberk Nov 23 '11 at 08:07
  • Look here http://stackoverflow.com/questions/6508516/asp-net-mvc-3-unable-to-find-the-requested-net-framework-data-provider – Baidaly Feb 09 '12 at 06:03
  • Is your second project a class library project ? – Misi Nov 08 '12 at 12:46
  • I have something similar only with a MVC 4 project and I get :_'Unable to retrive metadata for 'ClassLibrary1.TableName1'. The specified named connection is either not found in the configuration, not inteneded to be used with the EntityClient provider, or not valid.'_ – Misi Nov 08 '12 at 12:47
  • Try this link, this might help even more for you: http://stackoverflow.com/a/14354851/1983024 – Jon P Jan 17 '13 at 01:10
  • Make sure you have added a reference to the appropriate database DLL, which I'm assuming is "System.Data.SqlServerCE". It's should be located in "%Program Files%\Microsoft SQL Server Compact Edition\v4.0\Desktop" – Justin May 30 '13 at 21:02
  • Take a look at my answer on a question describing a similar problem [http://stackoverflow.com/a/17789016/674700](http://stackoverflow.com/a/17789016/674700). – Alex Filipovici Jul 22 '13 at 14:03

11 Answers11

8

I had installed EF 6 which added:

<providers>
    <provider invariantName="System.Data.SqlClient" 
        type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

In my config file. I had other scaffolding issues and so decided to fall back to Ef 5. After uninstalling EF 6 and reinstalling EF 5 I deleted the from the config and then I was able to build my new controller. I discovered this by using the

PM> Update-Database –Verbose
gunr2171
  • 16,104
  • 25
  • 61
  • 88
user2888722
  • 81
  • 1
  • 5
2

Just copy the connection string information from your EF model project App.Config into your and watch that you do not repeat any sections (entityFramework section for example)

Tom
  • 373
  • 3
  • 10
  • This worked for me. My MVC5 web project is complaining about metadata for connection string. Just copied connection string line from EF6 project's app.config to web.config. Then I could add controller and scafolded view. – bob May 07 '15 at 01:59
1

I added this to my Global.Asax file so that a) the database was started immediately and b) the table definition was added. allowing the Controller to find the table definition:

 System.Data.Entity.Database.SetInitializer<ProjectName.DAL.DBConnectContextName>(new CreateDatabaseIfNotExists<ProjectName.DAL.DBConnectContextName>());
Brian
  • 3,653
  • 1
  • 22
  • 33
1

Try this:

<add name="Monsters2Entities" 

    connectionString="
      metadata=res://*/Monsters.csdl|
               res://*/Monsters.ssdl|
               res://*/Monsters.msl;
      provider=System.Data.SqlClient;
      provider connection string='
        data source=.;
        initial catalog=Monsters2;
        integrated security=True;
        pooling=False;
        multipleactiveresultsets=True;
        App=EntityFramework
      '" 
      providerName="System.Data.EntityClient" 
/>

I have replaced &quot; with '

hammar
  • 138,522
  • 17
  • 304
  • 385
cdiazal
  • 988
  • 11
  • 28
  • Changing the encoded ticks for literal ticks did not fix the problem. I believe the problem is with the semantics rather than the syntax. – Brent Arias Nov 24 '11 at 19:09
0

I found thjis helped when I had this problem:

Uninstalled the pre-release version of EF and installed EF5 Went to Sql Server Object Explorer and deleted the db. Rebuilt the solution used PM> Update-Database -Verbose

Perhaps the problem was with pre-release EF or just cleansing the palate was all it needed.

I'm quite new to all of these but this did let me use the Add Controller scaffolding tool with the "MVC controller with read/write actions and reviews, using Entity Framework" template selected.

ajw1970
  • 53
  • 2
  • 9
  • Welcome to So. probably a question is asked in the answer reply block. Please write a new question for such kind of queries. – Prahalad Gaggar Dec 27 '12 at 13:08
  • Thank you. I wasn't asking a question. Trying to offer assistance with the solution I found when facing the same problem. – ajw1970 Dec 28 '12 at 15:29
0

First of all you should check your connection string must be in your Web.Config file in the ROOT! of your project after that check if there is another connection string or not if there is another connection string replace it with your connection string.... This solved my problem which was same as your's....

Mamali
  • 31
  • 5
0

I managed to solve my problem by changing the edmx's code generation strategy to T4, adding a EF5 Db Context and replacing %edmxInputFile% in both the *.tt and *.Context.tt files.

0

Rename the Model class - worked for me

0

Are you sure to have a ConnectionString in your Web.Config file before to create a Controller ? Sometimes we create another project which contains the Entity DB Context and we forgot to add this same connectionString in the Web.Config inside the another project.

<connectionStrings>
    <add name="Monsters2Entities" ...  />
</connectionStrings>
Digital3D
  • 161
  • 8
0

I face this issue "Unable to retrieve metadata..." while I was dding a Controller. Solution for this is as follows.

  1. In Connection String change provider to "system.data.sqlclient" 2.or Remove the Connection String from the Web.config and Add controller and after adding controller again add the connection String.

It seems this is a bug in MVC. I hope with this you can go ahead.

-1

first need to install package entity framework

go to tool> Libray package manager> pakage manager console > :install entity framework