Enable Entity Framework 6 for MySql (C#) in WinForms of Microsoft Visual Studio 2013
Yesterday I knew that Entity Framework is another method to access database beside using Dataset or DataReader,then I tried to make Entity Framework 6 work for my MySql database server in MVS 2013.
I open a WinForms with .Net FrameWork 4.5.1. (so I only have App.config but no app/web config in the project) After I installed mysql-installer-community-5.7.3.0-m13.msi and
install EntityFramework package via
TOOLS menu -> Library Package Manager -> Manage NuGet Packages for Solution... -> Online -> (Search) EntityFramework (beware of version of this package and it should be version 6.0.2, if not then click Updates -> EntityFramework to update)
When I tried to add ADO.NET Entity Data Model via
Right click Project -> Add -> New Item -> ADO.NET Entity Data Model -> Generate from Database -> New Connection -> Data sources: -> Change...-> MySQL Database -> Fill in the Server name with server IP, Username and Password -> Choose the Database name-> Test Connection -> OK
Then Entity Connection string is generated -> Tick Save entity connection settings in App.Config as -> Next> ->
Which version of Entity Framework do you want to use? Have option Entity Framework 6.0 but you cannot use it because
"Your project references the latest version of Entity Framework; however, an Entity Framework database provider compatible with this version could not be found for your data connection. Exit this wizard, install a compatible provider, and rebuid your project before performing this action".
How to solve this?
By the way , if you install Entity Framework version 5 in Nuget Package then you might have option Entity Framework 5.0 here and you might success to use Entity Framework 5 but not the version 6.
Solution 1:
First of all, we don't even need to install the mysql-installer-community-5.7.3.0-m13.msi.
- Install the latest mysql-visualstudio-plugin
- Install the latest mysql-connector-net
- New C# .Net 4.5 Framework WinForms (for 4.0 it should work based on Does Entity Framework 6 support .NET 4.0? )
- Install 4 Nuget Packages (follow sequence, if you install Mysql.Data.Entities before EntityFramework, it will resolve dependency and install EntityFramework 6.0.2 but what we need is EntityFramework 6.1.0)
EntityFramework
Mysql.Data
Mysql.Data.Entities
Mysql.Web
5.If you have tag entityFramework in App.config, please comment it and insert new tag entityFramework in App.config after tag startup
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
6.Add ADO.NET Entity Data Model (as mentioned in question)
7.After Entity Connection string is generated (as mentioned in question) and Tick Save entity connection settings in App.Config as then click Next
8.Choose Your Database Object and Settings (Tables, Views, or Stored Procedures and Functions) (Don't have "Which version of Entity Framework do you want to use?" because I have only one Entity Framework 6.0 provider so direct skip the selection if my only provider is valid)
9.Finish
Congratulations ^^
By the way, you may need to add the .dll files
- MySql.Data.dll
- MySql.Data.Entity.EF6.dll
- MySql.Web.dll
inside this folder
C:\Program Files\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5 (32bit windows)
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5 (64bit windows)
as your project reference for further EF6 functions.
Solution 2:
I followed the instructions given by V-Shy and was having the same problem as LaRae White with the wizard shutting down on me. I'm running VS2015 and had just used the MySql installer to update to MySQL for Visual Studio v1.2.6 and Connector/NET v6.9.8.
What I eventually did to get it to work was this:
- Uninstall all the packages I had previously installed to solve this issue (EntityFramework, Mysql.Data, Mysql.Data.Entities, Mysql.Web)
- Added the following references found in C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies\v4.5 manually: MySql.Data.dll, MySql.Data.Entity.EF6.dll, and MySql.Web.dll
I hope that helps someone out.
Solution 3:
I spend entire day figuring out how to solve this problem and nothing helps. Apparently i figure out 1 things about which people don't mention
In the model.edmx properties there is entire "DDL Generation Template", by default it set to "SSDLToSQL10.tt (VS)" but need to be set to "SSDLToMySQL.tt (VS)"
What is more it was also producing an error:
Running transformation: System.NullReferenceException: Object reference not set to an instance of an object.
bla bla bla
line 93 c:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\SSDLToMySQL.tt
What leads me to the official bug in "MySQL for Visual Studio 1.1.3" which will be fixed in next build 1.1.4 which is not available so far.
However there is workaround fix:
Overwrite the "MySql.Data.Entity.EF6.dll" in
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies
by file from NET Connector in
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\