. NET Core, Docker, MySQL simple tour (ii)

Source: Internet
Author: User
Tags dotnet mysql client

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.