Using MySql with Entity Framework 4 and the Code-First Development CTP

Right, finally got it working with a few points of interest.

  • Cannot create a DB, must exist already
  • You have to create a connection string for each DB contest using the DBContext name (in the above example a connectionstring must exist with the name “NerdDinners”), not just a default one (else it will use SQL)
  • It will use the name of the DBSet name you use to define your context as the name of the table, so be careful when naming them.

All in all, a long road but there in the end

**Update
Another point to note, when deploying your MVC site using MySQL you will most like need also add a DataFactory to your web.config.
Usually because of the difference in MySql connectors out there and the versions of MySQL that are supported.
(answer found through other sources after much head scratching)
Just add:

  <system.data> 
    <DbProviderFactories> 
      <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.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> 
    </DbProviderFactories> 
  </system.data>

As a seperate section to your web.config making sure to set the version number of the MySQL.Data.dll you deploy with the site (also a good idea to “copy as local” your MySQL DLLs to ensure compatibility.

Leave a Comment