I. Creating a database table
Run database: Docker restart Local-mysql
Creating a Employees database table with a MySQL client
Table Name: Employees
Id:int (11) primary key, auto increment
Name:varchar (32)
Lastname:varchar (32)
Ii. Modification of Project.json
{
"Version": "1.0.0-*",
"Buildoptions": {
"Debugtype": "Portable",
"Emitentrypoint": True
},
"Dependencies": {
"Microsoft.entityframeworkcore": "1.0.1",
"MySql.Data.Core": "7.0.4-ir-191",
"MySql.Data.EntityFrameworkCore": "7.0.4-ir-191"
},
"Frameworks": {
"netcoreapp1.0": {
"Dependencies": {
"Microsoft.NETCore.App": {
"Type": "Platform",
"Version": "1.0.1"
}
},
"Imports": "Dnxcore50"
}
}
}
Third, add SampleContext.cs
Using Microsoft.entityframeworkcore;
Using MySQL.Data.EntityFrameworkCore.Extensions;
Namespace startmysql{
public class Employeescontext:D Bcontext {
Public Employeescontext (dbcontextoptions<employeescontext> options): Base (options) {}
Public dbset<employee> Employees {get; set;}
}
public static class Employeescontextfactory {
public static Employeescontext Create (string connectionString) {
var optionsbuilder = new dbcontextoptionsbuilder<employeescontext> ();
Optionsbuilder.usemysql (connectionString);
var context = new Employeescontext (optionsbuilder.options);
Context. Database.ensurecreated ();
return context;
}
}
public class Employee {
Public Employee () {}
public int Id {get; set;}
public string Name {get; set;}
public string LastName {get; set;}
}
}
Iv. Modification of Program.cs
Using System;
Namespace Startmysql
{
public class Program
{
public static void Main (string[] args)
{
Createemployee ();
}
public static void Createemployee () {
var entry = new Employee () {Name = "John", LastName = "Winston"};
using (var context = Employeescontextfactory.create (@ "server=localhost;userid=root;pwd=root;port=3306;database= Mysql;sslmode=none; "))
{
Context. ADD (entry);
Context. SaveChanges ();
}
Console.WriteLine ($ "Employee is saved in the database with ID: {entry. ID} ");
}
}
}
V. Implementation procedures
dotnet Restore
Dotnet Run
. NET Core, Docker, MySQL simple tour (ii)