How to Use the MySQL EntityFramework component to process MYSQL PaaS DB,

Source: Internet
Author: User

How to Use the MySQL EntityFramework component to process MYSQL PaaS DB,

MySQL Database on Azure is a MySQL cloud Database Service launched on the Azure platform. It is fully compatible with the MySQL protocol, it provides a fully managed database service featuring stable performance, Fast deployment, high availability, and high security. Customers can use common platforms and technologies supporting MySQL for development and integration. This article demonstrates how to use the MySQL EntityFramework component to operate MySQL PaaS DB.

System Environment/application information

ASP. NET 2005 Core/MYSQL EntityFrameWork Core

Code details

Install the EntityFrameWork Core Component in the VS 2015 Net Core environment. The code and test results are as follows:

WhereData1Context. csThe 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. csThe 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();        }    }}

 

Appsettings. jsonThe file is:

{    "ConnectionStrings": {        "SampleConnection": "server=XXXXXX.mysqldb.chinacloudapi.cn;userid=XXXXXX%YYYYYY;pwd=XXXXXXXXX;port=3306;database=xyudb;sslmode=none;"    }}

 

Project. jsonThe 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

Running 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

 

For more exciting work, click to view

You are welcome to talk with others.

Graduate school A Azurecommunity@qq.com

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.