MySQL Database on Azure is a MySQL cloud database service launched on Azure platform. By fully compatible with MySQL protocol, it provides users with a fully managed database service with stable performance, rapid deployment, high availability and high security. Customers can use common platforms and technologies to support MySQL for development and integration. This article demonstrates how to use the MySQL Entity Framework component to operate on MySQL PaaS DB.
System environment/application information
ASP.NET 2005 Core / MYSQL EntityFrameWork Core
Detailed code
In the VS 2015 Net Core environment, install the EntityFrameWork Core component, code and test results are as follows:
The Data1Context.cs file is:
using Microsoft.EntityFrameworkCore; using MySQL.Data.EntityFrameworkCore.Extensions; namespace ConsoleApp1 { /// <summary> /// The entity framework context with a data1 DbSet /// </summary> public class Data1Context : DbContext { public Data1Context(DbContextOptions<Data1Context> options) : base(options) { } public DbSet<Data1> Data1 { get; set; } } /// <summary> /// Factory class for EmployeesContext /// </summary> public static class Data1ContextFactory { public static Data1Context Create(string connectionString) { var optionsBuilder = new DbContextOptionsBuilder<Data1Context>(); optionsBuilder.UseMySQL(connectionString); //Ensure database creation var context = new Data1Context(optionsBuilder.Options); context.Database.EnsureCreated(); return context; } } /// <summary> /// A basic class for an Employee /// </summary> public class Data1 { public int Id { get; set; } public string Name1 { get; set; } } }
Program.cs file is:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; namespace ConsoleApp1 { public class Program { public static void Main(string[] args) { var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); var configuration = builder.Build(); string connectionString = configuration.GetConnectionString("SampleConnection"); // Create an employee instance and save the entity to the database var entry = new Data1() { Id = 3, Name1 = "XingBing" }; using (var context = Data1ContextFactory.Create(connectionString)) { context.Add(entry); context.SaveChanges(); } Console.WriteLine($"Data1 was 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;pwd=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" ] } } }
item design
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 introductory tutorial
For more exciting work, please click to see
Welcome interested friends to communicate more
Graduate student A zurecommunity@qq.com