LATEST EDIT
The bug has been fixed.
Chris' comment:
2015-11-07 and you can now get it all working without editing any files or doing weird stuff. Use the MySQL for Windows installer and include the Visual Studio support and the latest Connector.Net. Be sure to update after installing and you will get the latest of each (1.2.5 and 6.9.8). Use NuGet to install EntityFramework, MySql.Data, and MySql.Data.Entity. Finally, built
and enjoy code-first reverse engineering goodness by adding an Ado.Net Entity Model.
Original answer
I found out it's a bug from MySQL.
Here's the link explaining a workarround.
On your machine where VS 2013 is installed, VS plugin (1.1.3 GA) and Connector/Net
Close all VS instances before doing the steps.
On
a Windows Explorer window go to this path or wherever you installed you Connector/net binaries
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\
Copy the file:
MySql.Data.Entity.EF6.dll
And paste it to this folder
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies
If it asks you to overwrite it please do so.
You'll need admin rights in order to overwrite the file.
Then you can try again
to generate the script for your model.
It is important that you have the 1.1.3 version of the VS plugin installed since this workaround is for that.
Unfortunately it doesn't work for me, so I downgraded to entity framework 5 until they fix this.
EDIT
Finaly, it works now.
I had to add the 3 following DLLs :
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Data.dll
- C:\Program Files
(x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Data.Entity.EF6.dll
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Web.dll
Then I changed the EntityFramework part in the web config to :
Don't forget to REBUILD and you should be able to create a entity framework 6 model with MySQL.
IMPORTANT
Make sure you have installed MySQL for visual studio 1.1.3 and MySQL
connector .net 6.8.3
Tutorial built with .NET 5.0
Other versions available:
- .NET: .NET 6.0
- Node: Node.js
In this post we'll go
through the steps to connect a .NET API to MySQL using Entity Framework Core, and to create a MySQL database from code using EF Core migrations.
We'll start with an example .NET CRUD API from a tutorial I posted recently, it uses an EF Core InMemory database by default for testing, we'll update it to connect to a MySQL database and run EF Core migrations to auto generate the database and tables from code. For full details about the .NET CRUD API see
.NET 5.0 - CRUD API Example and Tutorial.
Tutorial Contents
- Tools required for this tutorial
- Download & Run the example .NET API
- Update API to use MySQL
- Create SQL Database with EF Core Migrations
- Restart .NET CRUD API
To follow the steps in this tutorial you'll need the
following:
- .NET SDK - includes the .NET runtime and command line tools.
- Visual Studio Code - code editor that runs on Windows, Mac and Linux. If you have a different preferred code editor that's fine too.
- C# extension for Visual Studio Code - adds support to VS Code for developing .NET applications.
- MySQL - you'll need access to running MySQL server instance for the API to connect to, it can be remote (e.g. Azure, AWS etc) or on your local machine. The Community Server is available for free from
https://dev.mysql.com/downloads/mysql/, ensure it is started so the API can connect to it. Installation instructions are available at https://dev.mysql.com/doc/refman/8.0/en/installing.html.
Download & Run the example .NET API
Follow these steps to download
and run the .NET CRUD API on your local machine with the default EF Core InMemory database:
- Download or clone the tutorial project code from https://github.com/cornflourblue/dotnet-5-crud-api
- Start the api by running
dotnet run
from the command line in the project root folder (where the WebApi.csproj file is located), you should see the message Now listening on: http://localhost:4000
. - You can test the API
directly with a tool such as Postman or hook it up with an example Angular or React app available.
Starting in debug mode
You can also start the application
in debug mode in VS Code by opening the project root folder in VS Code and pressing F5 or by selecting Debug -> Start Debugging from the top menu, running in debug mode allows you to attach breakpoints to pause execution and step through the application code. For detailed instructions including a short demo video see VS Code + .NET - Debug a .NET Web App in Visual Studio
Code.
Update .NET API to use MySQL
Add MySQL database provider from NuGet
Run the following command from the project root folder to install the EF Core database provider for MySQL from NuGet:
dotnet add package Pomelo.EntityFrameworkCore.MySql
Add connection string to app settings
Open the appsettings.json
file and add the entry "ConnectionStrings"
with a child entry for the MySQL connection string (e.g. "WebApiDatabase"
), the connection string should be in the format "server=[DB SERVER URL]; database=[DB NAME]; user=[USERNAME]; password=[PASSWORD]"
.
When
EF Core migrations generates the database, the database
value will be the name of the database created in MySQL.
The updated appsettings.json
file with the connection string should look something like this:
{
"ConnectionStrings": {
"WebApiDatabase": "server=localhost; database=dotnet-5-crud-api; user=testUser; password=testPass123"
},
"Logging": {
"LogLevel": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Update Data Context to Use MySQL
The DataContext
class located at /Helpers/DataContext.cs
is used for accessing application data through Entity Framework. It derives from the Entity Framework DbContext
class and has a public Users
property for accessing and managing user data.
Update
the OnConfiguring()
method to connect to MySQL instead of an in memory database by replacing options.UseInMemoryDatabase("TestDb");
with options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
.
The updated DataContext
class should look like this:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using WebApi.Entities;
namespace WebApi.Helpers
{
public class DataContext : DbContext
{
protected readonly IConfiguration Configuration;
public DataContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// connect to mysql with connection string from app settings
var connectionString = Configuration.GetConnectionString("WebApiDatabase");
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
public DbSet Users { get; set; }
}
}
Create SQL Database from code with EF Core Migrations
Install dotnet ef tools
The .NET Entity Framework Core tools (dotnet ef
) are used to generate EF Core migrations, to install the EF Core tools globally run dotnet tool install -g dotnet-ef
, or to update run dotnet tool update -g dotnet-ef
. For more info on EF
Core tools see https://docs.microsoft.com/ef/core/cli/dotnet
Add EF Core Design package from NuGet
Run the following command from the project root folder to install the EF Core design package, it provides cross-platform command line tooling support and is used to generate EF Core migrations:
dotnet add package Microsoft.EntityFrameworkCore.Design
Generate EF Core migrations
Generate new EF Core migration files by running
the command dotnet ef migrations add InitialCreate
from the project root folder (where the WebApi.csproj file is located), these migrations will create the database and tables for the .NET Core API.
Execute EF Core migrations
Run the command dotnet ef database update
from the project root folder to execute the EF Core migrations and create the database and tables in MySQL.
Check MySQL and you should now see your database with the tables Users
and __EFMigrationsHistory
.
Restart .NET CRUD API
Stop and restart the API with the command dotnet run
from the project root folder, you should see the message Now listening on: http://localhost:4000
and the API should now be connected to MySQL.
Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.
I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.
What databases does Entity Framework work with?
Entity Framework Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, PostgreSQL, and Azure Cosmos DB.
Does .NET work with MySQL?
MySQL Connector/NET enables you to develop . NET applications that require secure, high-performance data connectivity with MySQL. It implements the required ADO.NET interfaces and integrates into ADO.
How does Entity Framework connect to SQL database?
You can test the API directly with a tool such as Postman or hook it up with an example Angular or React app available..
Starting in debug mode. ... .
Add SQL Server database provider from NuGet. ... .
Add connection string to app settings. ... .
Update Data Context to Use SQL Server. ... .
Install dotnet ef tools..
Can I use MySQL with .NET core?
NET Core and MySQL are both free and open source technologies. The new ASP.NET Core can run on Linux and in Linux Containers, and MySQL is one of the easiest databases to get started with. This makes the combination of ASP.NET Core and MySQL a pretty compelling combination.