Unable to Retrieve Metadata

后端 未结 6 2174
傲寒
傲寒 2020-11-29 10:01

MVC4 + Entity Framework 4.4 + MySql + POCO/Code First

I\'m setting up the above configuration .. here are my classes:

namespace BTD.         


        
相关标签:
6条回答
  • 2020-11-29 10:41

    I tested also around this bug and saw an other problem.

    Following code is in my Web.config (without, it don't work):

    <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    

    Changed it to:

    <entityFramework>
    

    Works for me... add the scaffolding and then change it back

    0 讨论(0)
  • 2020-11-29 10:48

    The imesh suggesting almost solve my problem, but additionally I temporary commented line

    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    

    which was in DBContext class. And of course after creation controller this line should be uncommented and change back System.Data.SqlClient to MySql.Data.MySqlClient in config file.

    0 讨论(0)
  • 2020-11-29 10:49

    Using VS 2013, MySQL Connector/NET 6.9.6, Entity Framework 6, Web API 2.2 and MySQL server 5.7 I had to combine the answers to prevent the error about "Unable to retrieve metadata".

    To successfully add a controller to a .NET project that uses a MySQL connection, do the following:

    1. Temporarily add a connection string with System.Data.SqlClient as the providerName, and comment the one for MySQL. It doesn't matter whether the connection string is valid.
    2. Ensure that MySqlEFConfiguration isn't enabled in any way.
    3. Rebuild.

    About the second point, the MySQL documentation on using Connector/NET with EF6 states three possible ways to enable the MySqlEFConfiguration. Ensure that none of these are enabled while adding controllers using the VS template.

    1. Adding the DbConfigurationTypeAttribute on the context class:

    [DbConfigurationType(typeof(MySqlEFConfiguration))]

    1. Calling DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) at the application startup

    2. Set the DbConfiguration type in the configuration file:

    <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

    0 讨论(0)
  • 2020-11-29 10:50

    It seems that MVC4 Controller scaffolding is not properly recognizing MySql Connection String. Change the connection string as shown below when generating EF CRUD code for Controllers:

    <connectionStrings>
        <add name="BTDContext" connectionString="Data Source=host_name;Database=database_name;uid=user_id;pwd=password;" providerName="System.Data.SqlClient" /> 
    </connectionStrings>
    

    Change it back to standard when running the application:

    <connectionStrings>
        <add name="BTDContext" connectionString="Data Source=host_name;Database=database_name;uid=user_id;pwd=password;" providerName="MySql.Data.MySqlClient" /> 
    </connectionStrings>
    

    Note the change, provider name.

    0 讨论(0)
  • 2020-11-29 10:51

    Please try using the

    System.ComponentModel.DataAnnotations

    namespace along with the [Key] attribute on the EDM class members.

    It worked for me.

    0 讨论(0)
  • 2020-11-29 11:00

    I've been having the same problem using EF 4.3.1 and MySql Connecter/Net 6.6.4.0

    This worked for me, no need to connect to a different db or extra code in the Context class.

    Add this between the entityFramework tags in your web.config file when you want to build a scaffold:

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
    

    Then comment the above code out when you want to run migrations and vice versa.

    So you web.config will look like so:

    <entityFramework>
      <contexts>
        <context type="ApptManager.Models.AppointmentsManagerContext, ApptManager">
          <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[ApptManager.Models.AppointmentsManagerContext, ApptManager], [ApptManager.Migrations.Configuration, ApptManager]], EntityFramework" />
        </context>
      </contexts>
      <!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
        <parameters>
          <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
        </parameters>
      </defaultConnectionFactory>-->
    </entityFramework>
    

    This is quite ridiculous how .NET developers have to jump through some arduous hoops in order to get their code working.

    Working example

    https://github.com/dublinan/mvc-mysql-ef-example

    Links to my github project

    0 讨论(0)
提交回复
热议问题