Pomelo.EntityFrameworkCore.MySql is an Entity Framework Core provider built on top of MySqlConnector. It enables use the Entity Framework Core ORM with MySQL. Async functions in this library properly implement Async I/O at the lowest level, unlike providers based on Oracle's MySql.Data library which uses Sync I/O at the lowest level.
To add a NuGet.config
file in your solution root, then you can use the unstable packages:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="Pomelo" value="https://www.myget.org/F/pomelo/api/v3/index.json" />
<add key="nuget.org" value="https://www.nuget.org/api/v2" />
</packageSources>
</configuration>
Here is a console application sample for accessing a MySQL database using Entity Framework:
① We recommand you to set utf8
as your MySQL database default charset. The following statement will check your DB charset.
show variables like 'character_set_database';
② Put Pomelo.EntityFrameworkCore.MySql
into your project's .csproj
file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="1.1.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
</Project>
③ Implement some models, DbContext in Program.cs
. Then overriding the OnConfiguring of DbContext to use MySQL database. Besides, you can define a JsonObject field if you are using MySQL Server 5.7. Finally to invoking MySQL with EF Core in your Main() method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace MySqlTest
{
public class User
{
public int UserId { get; set; }
[MaxLength(64)]
public string Name { get; set; }
}
public class Blog
{
public Guid Id { get; set; }
[MaxLength(32)]
public string Title { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public virtual User User { get; set; }
public string Content { get; set; }
public JsonObject<List<string>> Tags { get; set; } // Json storage (MySQL 5.7 only)
}
public class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseMySql(@"Server=localhost;database=ef;uid=root;pwd=123456;");
}
public class Program
{
public static void Main()
{
using (var context = new MyContext())
{
// Create database
context.Database.EnsureCreated();
// Init sample data
var user = new User { Name = "Yuuko" };
context.Add(user);
var blog1 = new Blog {
Title = "Title #1",
UserId = user.UserId,
Tags = new List<string>() { "ASP.NET Core", "MySQL", "Pomelo" }
};
context.Add(blog1);
var blog2 = new Blog
{
Title = "Title #2",
UserId = user.UserId,
Tags = new List<string>() { "ASP.NET Core", "MySQL" }
};
context.Add(blog2);
context.SaveChanges();
// Changing and save json object #1
blog1.Tags.Object.Clear();
context.SaveChanges();
// Changing and save json object #2
blog1.Tags.Object.Add("Pomelo");
context.SaveChanges();
// Output data
var ret = context.Blogs
.Where(x => x.Tags.Object.Contains("Pomelo"))
.ToList();
foreach (var x in ret)
{
Console.WriteLine($"{ x.Id } { x.Title }");
Console.Write("[Tags]: ");
foreach(var y in x.Tags.Object)
Console.Write(y + " ");
Console.WriteLine();
}
}
Console.Read();
}
}
}
By viewing the following full project which is a single-user blog system and based on this library(MySQL for Entity Framework Core) to explorer more features: View on GitHub.
Milestone | Release week |
---|---|
1.0.0 | Aug 5th 2016 |
1.0.1 | Oct 29th, 2016 |
1.1.0 | Nov 25th, 2016 |
Support all Entity Framework Core operations, basic json field support.
- Switch ADO.NET layer to MySqlConnector
- Improve Performance
Upgrade to .NET Core 1.1 and EF 1.1.0, which supports Explicit Loading. Implement Design-time layer for supporting scaffolding.
① Add Design-time layer and EF Core tools to your project (>= 1.1.0-rtm-10004):
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="1.1.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
</Project>
② Using the tool to execute scaffolding commands
dotnet ef dbcontext scaffold "Server=localhost;User Id=root;Password=123456;Database=eftests" "Pomelo.EntityFrameworkCore.MySql"
One of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.