0

I get the 'Unable to retrieve metadata' error whenever I try to create a controller for my MVC 3 Application (Razor - C# - Visual Studio 2012).

I have done some research on this and other sites in order to fix this. What I have done so far is:

  • Mapped Model Class with the exact same fields as the MYSQL table
  • Added the [Key] Annotation (using System.ComponentModel.DataAnnotations)
  • Deleted And Created .edmx file various times
  • Restarted VS 2012
  • Copied my existing project into a brand new solution

Probably I'm missing something, being new to the .NET framework altogether

Here are the contents of my Web.config file:

<?xml version="1.0" encoding="utf-8"?>
<!--
  Para obtener más información sobre cómo configurar la aplicación de ASP.NET, visite
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>

  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>   
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <httpRuntime targetFramework="4.5" />
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
    <!--
            If you are deploying to a cloud environment that has multiple web server instances,
            you should change session state mode from "InProc" to "Custom". In addition,
            change the connection string named "DefaultConnection" to connect to an instance
            of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
      -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
      </provider>
    </providers></entityFramework>
<connectionStrings>
  <add name="DeporteYaEntities" connectionString="metadata=res://*/DeporteYa.csdl|res://*/DeporteYa.ssdl|res://*/DeporteYa.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;password=1234;database=deporteya&quot;"
    providerName="System.Data.EntityClient" />
  <add name="usuario__DeporteYaEntities_" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=usuario__DeporteYaEntities_-20160610143458; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|usuario__DeporteYaEntities_-20160610143458.mdf"
    providerName="System.Data.SqlClient" />
</connectionStrings><system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data></configuration>

Screen capture of the exact error:

enter image description here

Once again, any help will be greatly appreciated. Many thanks!

EDIT:

I have changed the connection string in Web.config file as suggested in this post to:

connectionString="metadata=res://*/;

Now it returns a different error:

'Unable to retrieve metadata for DeporteYa.Models.Usario. Unable to cast object of type 'System.Data.Entity.Core.Objects.ObjectContext' to 'System.Data.Objects.ObjectContext'
Community
  • 1
  • 1
Juan M
  • 4,063
  • 4
  • 19
  • 28
  • update your code with usuario model – MMM Jun 10 '16 at 18:09
  • Possible duplicate of [MetadataException: Unable to load the specified metadata resource](http://stackoverflow.com/questions/689355/metadataexception-unable-to-load-the-specified-metadata-resource) – mohsen Jun 10 '16 at 18:17
  • can you post the `usuario` model in your question? it would greatly help us – Grizzly Jun 10 '16 at 18:20
  • why do you have 2 connection strings? – Grizzly Jun 10 '16 at 18:23
  • what version of entity framework are you using? – Grizzly Jun 10 '16 at 18:39
  • @BviLLe_Kid I'm using EF 6.1.3. I also deleted the second connection string, as you sugested. – Juan M Jun 10 '16 at 18:40
  • okay that could be the problem, I know that MVC4 doesn't support EF 6, so MVC3 probably doesn't either.. so go to Nuget where I assume you downloaded EF and uninstall it.. then reinstall EF5 – Grizzly Jun 10 '16 at 18:49

2 Answers2

1

i had a similar situation, but what i did was remodel the solution. In my case, i had an object with 3 foreign keys. 1 0f the foreign keys was a on a 1-1 relationship.

i moved the key on the 1-1 relationship to the other object and problem was solved

0

UPDATE:

The reason as to why you are experiencing the 2nd error message after you updated the question is because EF6 isn't supported by MVC4.. so I doubt MVC3 will support it either.

So to resolve this, you are going to have to go through Nuget (which is where I assume you downloaded EF in the first place), uninstall EF6 and then reinstall EF5

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • This is the correct answer. I wasn't aware of all this. Tried uninstalling EF 6 and its dependencies, then installing EF 5 and getting rid of the tag in the Web.config File. Worked perfectly. Thanks!!!! – Juan M Jun 10 '16 at 19:06