Entity Framework 6 CodeFirst with Oracle and MySql

c# entity-framework-6 mysql oracle11g


I'm trying use CodeFirst and Entity Framework 6 because I need it to use MVC 5. I have existing database Schema on MySQL and Oracle, both with the same schema (same tables, fields, restrictions ...)

I have installed Entity Framework 6.1.3 Tools for Visual Studio 2012 to generate the Code First from Database. And I have also added MySQL.Data.Entities from Nugget.

When I'm using the wizard to generate the Codefirst from Database after selecting the connection I'm getting the following error (translated from Spanish)

Your project is referencing to the last version of EntityFramework. But Entity Framework database provider compatible with this version cannot be found. If you have already installed a compatible provider be sure to compile the project before doing this action. In other case exit from the wizard, install a compatible provider and compile the project before doing this action.

I want to do it with code first because i think this is better for working with two different providers (Oracle and MySQL). The solution have to run on both Oracle and MySQL depending on the scenario by changing the the connectionString.

The Server versions are MySQL 5.6.19 and Oracle 11g.

9/8/2015 9:22:24 AM

Popular Answer

Viewing that the wizard of Visual Studio is not working I'm staring to do the process manually and I want to show it to help others with the same problem.

I have added MySQL.Entities.EF6 and Oracle.ManagedDataAccess from nugget and them I have used the update-package command in nugget.

In App.config I have to add the DBProvider factories.

  <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=, Culture=neutral, PublicKeyToken=***" />
  <remove invariant="Oracle.ManagedDataAccess.Client" />
  <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=, Culture=neutral, PublicKeyToken=***" />

And In Entityframework section of App.config you have to add also the providers.

  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
  <provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework" />

Of course you need also de connection strings of your Database.

Them you have to implement your context like this. For Oracle you have to specify the Schema Name. For me it doesn't work without it. In the Context you have to add one DBSet Property for each Entity you have.

namespace EFCodeFirst.DataModel
    public partial class Entities : DbContext
        //EntitiesOracle -> Name of the connection String
        public Entities()
            : base("name=EntitiesOracle")

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
            //For Oracle is neccesary

        public DbSet<T_TABLE> T_TABLE { get; set; }

The last thing you have to do is create you entity T_TABLE. You can Decorate the properties of the Entity with DataAnnotations. In my case I decided to use the Entities created with Visual Studio using Database First wizard and them I have copied all to modify them with DataAnnotations, saving me create entities with their property.

public partial class T_TABLE
    public int ID { get; set; }

    public string NAME { get; set; }

Hoping this helps you if you have the same problem as me. This way is too manually but it is working well for me.

9/13/2015 9:23:31 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow