5

This walkthrough works great with SQL Express: http://msdn.microsoft.com/en-us/library/gg197522(v=VS.103).aspx

I would like it to work with MySQL. I've done some research but none of the techniques I've found has been able to do it for me. Ideally I would like to do something like this:

      <entityFramework>
    <defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
  </entityFramework>

This doesn't work (I have MySQL Connector Net 6.5.4 installed & MySql.Data referenced). I've tried deriving from IDbConnection factory as shown in this class here: http://www.vworker.com/RentACoder/misc/BidRequests/ShowBidRequest.asp?lngBidRequestId=1563829

and then using:

      <entityFramework>
<defaultConnectionFactory type="SchoolModel.MySqlConnectionFactory, SchoolModel" />

but that doesn't work either. Can anybody please give me some pointers as to how to get this to work?

Many thanks.

3
  • It doesn't work is not a description of the problem. Do you receive any exception? What is an exception stack trace and message? Commented May 18, 2012 at 9:10
  • It doesn't work means that the techniques described above do not connect the code to the MySQL database. No exceptions, the application continues it's connection to the default SQLEXPRESS database. Thank you for your reply! Commented May 21, 2012 at 8:38
  • Just to verify the basics, do you have your connection string properly setup to connect to MySQL, or did you leave that at its defaults (which would be either SQL Express or SQL CE)? Commented Jun 2, 2012 at 19:20

3 Answers 3

3

To use Connector 6.5.4 with code-first EF5 on VS2012 you need:

  1. Install MySql Connector 6.5.4 msi
  2. Open VS2012 x86 Command Prompt as Admin and execute:

    gacutil /i "C:\Program Files (x86)\MySQL\Connector NET 6.5.4\Assemblies\v4.0\mysql.data.dll" gacutil /i "C:\Program Files (x86)\MySQL\Connector NET 6.5.4\Assemblies\v4.0\mysql.data.entity.dll"

  3. Add in your project's App.config this code to <configuration> section:

    <system.data> 
        <DbProviderFactories> 
            <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=6.5.4.0, Culture=neutral, 
                PublicKeyToken=c5687fc88969c44d"
            /> 
        </DbProviderFactories> 
    </system.data>
    
  4. Now add references to MySql.Data and MySql.Data.Entity to your solution and some code like this (I create MySqlConnection, then pass it to constructor of MyDbContext)

    public class MyDbContext : DbContext
    {
        public MyDbContext(DbConnection connection) : base(connection, true) { }    ​
    
        public DbSet<Product> Products { get; set; }
    }
    
    [Table("sund_jshopping_products")]
    public class Product
    {
        [Key]
        [Column("product_id")]
        public int Id { get; set; }
        [Column("product_ean")]
        public string Ean { get; set; }
        [Column("product_manufacturer_id")]
        public int OperatorId { get; set; }
        [Column("months_status")]
        public string MonthsStatus { get; set; }
        [Column("extra_field_5")]
        public string SideId { get; set; }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Can you give an example on how you create a MySql DbConnection object? Also, have you upgraded to MySql Connector 6.6.5.0 and come up with a different way to do it where you can specify everything in app.config?
1

Connector 6.5.4 does not support code-first with EF 5. Actually it does not support code first.

You can try using dot net connector (at least the trial version).

Comments

0

Did you set the Datasource to MySQL while setting up your db connection. Also, click and make sure "Test Connection" succeeds before trying a connection directly from the code.

enter image description here

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.