locked
Problem with using Mysql connector RRS feed

  • Question

  • User-1604844928 posted

    Currently I am using Mysql 6.3.9 connector. It supports ADO.NET Entity Data Model with Mysql Database.
    But if I change Mysql connector to 6.8.3 it does not support ADO.NET Entity data model with Mysql Database.
    It supports ADO.NET Entity data model For SQL and Oracle Database only.

    I don't understand why updated connector is not supporting ADO.NET Entity Data Model with Mysql Database.

    Thursday, January 21, 2016 8:36 AM

Answers

  • User-718146471 posted

    Chances are it is not picking up your connection string post upgrade. Take a look at this thread:

    http://stackoverflow.com/questions/20602114/mysql-connector-6-8-2-rc-entity-framework-6-and-code-first

    "I had MySQL EF6 and Migrations up and working when everything was in one MVC project. I split it out into layers (Core[Interfaces/Entitites], Data, Services, and Web) and started getting the same error Loren mentioned. Figured out that it wasn't picking up the connection string from the MVC app. Turns out that all I had to do was re-create the connection string within the App.config in my Data project (where the DbContext and mappings reside).

    These are the steps I took to get everything working:

    Step 1) Use NuGet to import MySql.Data.Entities (current version as of this post is 6.8.3.0)

    Step 2) Add the following to App.config and/or Web.config: 

    <connectionStrings>
         <add name="MyDB" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost; port=3306;  
         Initial Catalog=mydb; uid=myuser; pwd=mypass;" /> 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> 
         <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, 
         EntityFramework.SqlServer" /> 
              <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,      
         MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
              </provider> 
         </providers> 
    </entityFramework>

    Step 3) Set up your DbContext to use MySql:

    using MyApp.Core.Entities.Directory;
    using MyApp.Data.Mapping;
    using System.Data.Entity;
    
    namespace MyApp.Data
    {
        [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
        public class MyContext : DbContext
        {
            public MyContext() : this("MyDB") { }
            public MyContext(string connStringName) : base(connStringName) {}
            static MyContext ()
            {
                // static constructors are guaranteed to only fire once per application.
                // I do this here instead of App_Start so I can avoid including EF
                // in my MVC project (I use UnitOfWork/Repository pattern instead)
                DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration());
            }
    
            public DbSet<Country> Countries { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                // I have an abstract base EntityMap class that maps Ids for my entities.
                // It is used as the base for all my class mappings
                modelBuilder.Configurations.AddFromAssembly(typeof(EntityMap<>).Assembly);
                base.OnModelCreating(modelBuilder);
            }
        }
    }

    Step 4) Set the Default Project to your Data project in the Package Manager Console

    Step 5) Use enable-migrations, add-migration, update-database like you normally would

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 21, 2016 1:19 PM