DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Building and Integrating REST APIs With AWS RDS Databases: A Node.js Example
  • Java EE 6 Pet Catalog with GlassFish and MySQL
  • From Naked Objects to Naked Functions
  • Working With Transactions in Entity Framework Core and Entity Developer

Trending

  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Solid Testing Strategies for Salesforce Releases
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  1. DZone
  2. Data Engineering
  3. Databases
  4. Getting MySQL work with Entity Framework 4.0

Getting MySQL work with Entity Framework 4.0

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Dec. 10, 10 · News
Likes (0)
Comment
Save
Tweet
Share
24.2K Views

Join the DZone community and get the full member experience.

Join For Free

Does MySQL work with Entity Framework 4.0? The answer is: yes, it works! I just put up one experimental project to play with MySQL and Entity Framework 4.0 and in this posting I will show you how to get MySQL data to EF. Also I will give some suggestions how to deploy your applications to hosting and cloud environments.

MySQL stuff

As you may guess you need MySQL running somewhere. I have MySQL installed to my development machine so I can also develop stuff when I’m offline. The other thing you need is MySQL Connector for .NET Framework. Currently there is available development version of MySQL Connector/NET 6.3.5 that supports Visual Studio 2010.

Before you start download MySQL and Connector/NET:

  • MySQL Community Server
  • Connector/NET 6.3.5

If you are not big fan of phpMyAdmin then you can try out free desktop client for MySQL – HeidiSQL. I am using it and I am really happy with this program.

NB! If you just put up MySQL then create also database with couple of table there. To use all features of Entity Framework 4.0 I suggest you to use InnoDB or other engine that has support for foreign keys.

Connecting MySQL to Entity Framework 4.0

Now create simple console project using Visual Studio 2010 and go through the following steps.

1. Add new ADO.NET Entity Data Model to your project.

For model insert the name that is informative and that you are able later recognize.

Adding ADO.NET Entity Data Model

Now you can choose how you want to create your model. Select “Generate from database” and click OK.

Choosing source of model contents

2. Set up database connection

Change data connection and select MySQL Database as data source. You may also need to set provider – there is only one choice. Select it if data provider combo shows empty value.

Changing data source

Click OK and insert connection information you are asked about. Don’t forget to click test connection button to see if your connection data is okay.

Setting connection properties

If everything works then click OK.

3. Insert context name

Now you should see the following dialog. Insert your data model name for application configuration file and click OK.

Data connection settings

Click next button.

4. Select tables for model

Now you can select tables and views your classes are based on. I have small database with events data. Uncheck the checkbox “Include foreign key columns in the model” – it is damn annoying to get them away from model later. Also insert informative and easy to remember name for your model.

Select tables for model

Click finish button.

5. Define your classes

Now it’s time to define your classes. Here you can see what Entity Framework generated for you. Relations were detected automatically – that’s why we needed foreign keys. The names of classes and their members are not nice yet.

Automatically created classes

After some modifications my class model looks like on the following diagram. Note that I removed attendees navigation property from person class.

Final version of classes

Now my classes look nice and they follow conventions I am using when naming classes and their members.

NB! Don’t forget to see properties of classes (properties windows) and modify their set names if set names contain numbers (I changed set name for Entity from Entity1 to Entities).

6. Let’s test!

Now let’s write simple testing program to see if MySQL data runs through Entity Framework 4.0 as expected. My program looks for events where I attended.


using(var context = new MySqlEntities())
{
    var myEvents = from e in context.Events
                    from a in e.Attendees
                    where a.Person.FirstName == "Gunnar" &&
                            a.Person.LastName == "Peipman"
                    select e;
 
    Console.WriteLine("My events: ");
 
    foreach(var e in myEvents)
    {
        Console.WriteLine(e.Title);
    }
}
 
Console.ReadKey();

MySQL Entity Framework test succeededAnd when I run it I get the result shown on screenshot on right. I checked out from database and these results are correct.

At first run connector seems to work slow but this is only the effect of first run. As connector is loaded to memory by Entity Framework it works fast from this point on.

Now let’s see what we have to do to get our program work in hosting and cloud environments where MySQL connector is not installed.

Deploying application to hosting and cloud environments

If your hosting or cloud environment has no MySQL connector installed you have to provide MySQL connector assemblies with your project. Add the following assemblies to your project’s bin folder and include them to your project (otherwise they are not packaged by WebDeploy and Azure tools):

  • MySQL.Data
  • MySQL.Data.Entity
  • MySQL.Web

You can also add references to these assemblies and mark references as local so these assemblies are copied to binary folder of your application. If you have references to these assemblies then you don’t have to include them to your project from bin folder.

Also add the following block to your application configuration file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<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.2.0.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d”
/>
</DbProviderFactories>
</system.data>
...
</configuration>

Conclusion

It was not hard to get MySQL connector installed and MySQL connected to Entity Framework 4.0. To use full power of Entity Framework we used InnoDB engine because it supports foreign keys. It was also easy to query our model. To get our project online we needed some easy modifications to our project and configuration files.

Database connection Entity Framework MySQL Framework

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building and Integrating REST APIs With AWS RDS Databases: A Node.js Example
  • Java EE 6 Pet Catalog with GlassFish and MySQL
  • From Naked Objects to Naked Functions
  • Working With Transactions in Entity Framework Core and Entity Developer

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!