Connect to MySql EF core

CREATE A table

CREATE TABLE IF NOT EXISTS employees (
Id int(11) NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
PRIMARY KEY (Id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


— Dumping data for table employees

INSERT INTO employees (Id, first_name, last_name) VALUES
(1, ‘aaaa’, ‘cccc’),
(2, ‘ssss’, ‘cccc’);

#project.json
{
“version”: “1.0.0-*”,
“buildOptions”: {
“debugType”: “portable”,
“emitEntryPoint”: true
},
“tools”: {
“Microsoft.EntityFrameworkCore.Tools”: “1.0.0-preview3-final”
},
“dependencies”: {
“NETStandard.Library”: “1.6.1”,
“Microsoft.EntityFrameworkCore”: “1.1.0”,
“Microsoft.EntityFrameworkCore.SqlServer”: “1.0.0”,
“MySql.Data.EntityFrameworkCore”: “7.0.4-ir-191”,
“Pomelo.EntityFrameworkCore.MySql”: “1.1.0”,
“Pomelo.EntityFrameworkCore.MySql.Design”: “1.1.0”,
“Microsoft.EntityFrameworkCore.Design”: {
“version”: “1.0.0-preview2-final”,
“type”: “build”
},
“Microsoft.NETCore.App”: {
“type”: “platform”,
“version”: “1.0.0”
}
},
“frameworks”: {
“netcoreapp1.0”: {
“dependencies”: {
“Microsoft.NETCore.App”: {
“type”: “platform”,
“version”: “1.0.1”
},
“MySql.Data.Core”: “7.0.4-IR-191”
},
“imports”: “dnxcore50″
}
}
}

#.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApplication
{
public class Employee
{
public int Id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }

}
public class BookDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.U(@”Server=.;Database=log;Persist Security Info=True;User ID=sa;Password=pass”);
optionsBuilder.UseMySql(@”server=localhost;user id=root;password=;port=3306;database=test;SslMode=None”);
}
public DbSet Employees { get; set; }

}

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(“Hello World!”);
using (var context = new BookDbContext())
{
// Create database
// context.Database.EnsureCreated();

List emps = context.Employees.ToList();
}
System.Console.ReadKey();
}
}
}

#ref
https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

Leave a Reply

Your email address will not be published. Required fields are marked *