MySQL database on azure is the MySQL cloud databases service launched on the Azure platform, providing users with a fully-managed, fast-deployable, highly available, high-security database service with full MySQL protocol compatibility. Customers can use common MySQL-enabled platforms and technologies for development and integration. This article demonstrates how to use the MySQL EntityFramework component to operate MySQL PaaS DB.
System Environment/Application information
ASP. 2005 Core/mysql EntityFramework Core
Detailed code
In the VS. Net core Environment, the EntityFramework core components are installed, and the code and test results are as follows:
Where the Data1Context.cs file is:
usingMicrosoft.entityframeworkcore;usingMySQL.Data.EntityFrameworkCore.Extensions;namespaceconsoleapp1{/// <summary> ///The Entity Framework context with a data1 DbSet/// </summary> Public classData1context:dbcontext { PublicData1context (dbcontextoptions<data1context>options):Base(options) {} PublicDbset<data1> Data1 {Get;Set; } } /// <summary> ///Factory class for Employeescontext/// </summary> Public Static classData1contextfactory { Public StaticData1context Create (stringconnectionString) { varOptionsbuilder =NewDbcontextoptionsbuilder<data1context>(); Optionsbuilder.usemysql (connectionString); //Ensure database Creation varContext =NewData1context (optionsbuilder.options); Context. Database.ensurecreated (); returncontext; } } /// <summary> ///A Basic class for A Employee/// </summary> Public classData1 { Public intId {Get;Set; } Public stringName1 {Get;Set; } }}
The Program.cs file is:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMicrosoft.Extensions.Configuration;namespaceconsoleapp1{ Public classProgram { Public Static voidMain (string[] args) { varBuilder =NewConfigurationbuilder (). Addjsonfile ("Appsettings.json", Optional:false, Reloadonchange:true); varConfiguration =Builder. Build (); stringconnectionString = Configuration. GetConnectionString ("sampleconnection"); //Create an employee instance and save the entity to the database varEntry =NewData1 () {Id =3, Name1 ="xingbing" }; using(varContext =data1contextfactory.create (connectionString)) {context. ADD (entry); Context. SaveChanges (); } Console.WriteLine ($"Data1 is saved in the database with ID: {entry. ID}"); Console.readkey (); } }}
The appsettings.json file is:
{ "ConnectionStrings": { " Sampleconnection""server=xxxxxx.mysqldb.chinacloudapi.cn;userid=xxxxxx%yyyyyy ;p Wd=xxxxxxxxx;port=3306;database=xyudb;sslmode=none; " }}
The project.json file is:
{ "version":"1.0.0-*", "buildoptions": { "Debugtype":"Portable", "Emitentrypoint":true, "Copytooutput": { "include":"Appsettings.json" } }, "Dependencies": { "Microsoft.Extensions.Configuration":"1.0.0", "Microsoft.Extensions.Configuration.Json":"1.0.0", "Microsoft.entityframeworkcore":"1.0.0", "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.0" } }, "Imports": [ "Dnxcore50", "portable-net452+win81" ] } }}
Project composition
Operation and test results
Component Address
MySql.Data.EntityFrameworkCore 7.0.7-m61
Reference method
MySQL EF Core provider and connector/net 7.0.4 Getting Started tutorial
More wonderful work please click to view
Interested friends are welcome to communicate
A research institute graduate student [email protected]
How to use MySQL entityframework component to process MySQL PaaS DB