Entity Framework 6 and MySQL, model first

16,071

It seems like you may be using the wrong providerName in your connection string, it should be System.Data.EntityClient.

Here's how I finally got my model-first code to work with MySQL and what my app.config looks like:

<connectionStrings>
  <add name="MyEntities"
       connectionString="metadata=res://*/Data.Entity.Model.csdl|res://*/Data.Entity.Model.ssdl|res://*/Data.Entity.Model.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;Data Source=localhost;User Id=dbadmin;Password=password;Initial Catalog=database_name;&quot;"
       providerName="System.Data.EntityClient"/>
</connectionStrings>

In case that doesn't work, here's the entityFramework section. You'll notice the codeConfigurationType attribute I used in the following section. It is adds dependency resolvers for MySQL, but there are other ways of doing it which you can read about in the MySQL: EF 6 Support documentation.

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
  <providers>
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
  </providers>
</entityFramework>
Share:
16,071

Related videos on Youtube

Johan Svensson
Author by

Johan Svensson

Updated on September 29, 2022

Comments

  • Johan Svensson
    Johan Svensson over 1 year

    I have a problem using Entity Framework 6 with MySQL and doing so using model/database-first technique in ASP.NET C# MVC 3.

    The current situation is that I’m getting the error:

    Keyword not supported.
    Parameter name: metadata
    

    The metadata is specified in the connection string in the web.config –file:

    <add name="SiteNameContainer" 
      connectionString="metadata=res://*/Models. SiteName.csdl|
        res://*/Models. SiteName.ssdl|
        res://*/Models. SiteName.msl;
        provider=MySql.Data.MySqlClient;
        provider connection string='server=127.0.0.1;
        user id=fire;password=fire_db;
        database=fire_dotnet'" 
      providerName="MySql.Data.MySqlClient" />
    

    I tried to remove the metadata-section in the connectionString, but then its saying that the keyword “provider” is not supported, and then “provider connection string” not supported.

    I also have these 2 sections in my web.config file:

    <entityFramework>
        <providers>
            <provider invariantName="MySql.Data.MySqlClient" 
                type="MySql.Data.MySqlClient.MySqlProviderServices, 
                MySql.Data.Entity.EF6, Version=6.8.3.0, 
                Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        </providers>
    </entityFramework>
    <system.data>
    <DbProviderFactories>
        <clear />
        <remove invariant="MySql.Data.MySqlClient" />
        <add name="MySql.Data.MySqlClient" 
            invariant="MySql.Data.MySqlClient" 
            description=".Net Framework Data Provider for MySQL" 
            type="MySql.Data.MySqlClient.MySqlClientFactory, 
            MySql.Data, Version=6.8.3.0, Culture=neutral, 
            PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
    </system.data>
    

    I have references to MySQL Net-connector 6.8.3 files (MySql.Data, MySql.Data.Entity.EF6, MySql.Web).

    My requirments are that I must use MySQL and Entity Framework 6, and the changelog for the .NET connectior says that 6.8.x added support for MySQL.

    I’m out of ideas, hopefully someone can help me with this. Thanks in advance.

    • Steve
      Steve over 10 years
      Have you looked at dev.mysql.com/doc/refman/5.0/en/… yet? The example app.config there is significantly different than what you posted. Full disclosure: I've never used EF with MySQL.
  • Daryl Van Sittert
    Daryl Van Sittert about 10 years
    Aha! Just the one line fixed it for me <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/> Thanks!