MySQL Database on Azure 是 Azure 平台上推出的 MySQL 云数据库服务,通过全面兼容 MySQL 协议,为用户提供了一个全托管的性能稳定、可快速部署、高可用、高安全性的数据库服务。客户可以使用常见的支持 MySQL 的平台与技术进行开发与集成。本文演示了如何使用 MySQL EntityFramework 组件对 MySQL PaaS DB 进行操作。

系统环境 / 应用程序信息

ASP.NET 2005 Core / MYSQL EntityFrameWork Core

详细代码

在 VS 2015 Net Core 的环境中,安装 EntityFrameWork Core 组件,代码和测试后结果如下:

其中 Data1Context.cs 文件为:

 
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 文件为:

 
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.json 文件为:

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

project.json 文件为:

 
{
"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"
]
}
}
}

项目组成

运行及测试结果

组件地址

MySql.Data.EntityFrameworkCore 7.0.7-m61

参考方法

MySQL EF Core provider and Connector/Net 7.0.4 入门教程

立即访问http://market.azure.cn

【数据库-MySQL on Azure】如何使用 MySQL EntityFramework 组件处理 MYSQL PaaS DB的更多相关文章

  1. 如何使用 MySQL EntityFramework 组件处理 MYSQL PaaS DB

    MySQL Database on Azure 是 Azure 平台上推出的 MySQL 云数据库服务,通过全面兼容 MySQL 协议,为用户提供了一个全托管的性能稳定.可快速部署.高可用.高安全性的 ...

  2. ruby使用DBI连接MySQL数据库发生异常:in `error': Can't connect to MySQL server on 'localhost' (10061) (DBI::DatabaseError)

    Ruby使用DBI连接MySQL数据库一般为: require "dbi" dbh = DBI.connect("dbi:Mysql:test:localhost&quo ...

  3. mysql启动停止,一台服务器跑 多个mysql数据库

    一.以非特权用户运行MySQL服务器在讨论如何启动MySQL服务器之前,让我们考虑一下应该以什么用户身份运行MySQL服务器.服务器可以手动或自动启动.如果你手动启动它, 服务器以你登录Unix(Li ...

  4. bugfree,CDbConnection 无法开启数据库连线: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.0.99' (4)

    安装bugfree后,访问报错:CDbConnection 无法开启数据库连线: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '19 ...

  5. Mysql 利用拷贝data目录文件的方式迁移mysql数据库

    Mysql 利用拷贝data目录文件的方式迁移mysql数据库 步骤如下: 1.首先要确定data目录 这个问题困扰了我很久,因为网上的帖子大部分只是说拷贝mysql数据库目录下的data文件夹中的数 ...

  6. mysql分享一:运维角度浅谈MySQL数据库优化

    转于:http://lizhenliang.blog.51cto.com/7876557/1657465 1.数据库表设计要合理避免慢查询.低效的查询语句.没有适当建立索引.数据库堵塞(死锁)等 2. ...

  7. Linux下XAMPP装完之后,Navicat无法连上数据库的问题的解决 注意'mypassword'是当前的mysql登录密码

    Linux下装完XAMPP之后,mysql是自带装好了的,这个时候,mysql的root用户没有密码. 在mac 下安装好xampp后,需要在终端命令行操作时,比如输入:mysql -u root - ...

  8. 第六阶段·数据库MySQL及NoSQL实践第1章·章节一MySQL数据库

    01 课程介绍 02 数据库管理系统介绍 03 MySQL安装方式介绍及源码安装 04 MySQL安装后的基本配置 05 MySQL体系结构-服务器.客户端模型 06 MySQL体系结构-实例.连接层 ...

  9. 【数据库】4.0 MySQL入门学习(四)——linux系统环境下MySQL安装

    1.0 我的操作系统是CentOS Linux release 7.6.1810  (Core) 系统详细信息如下: Linux version 3.10.0-957.1.3.el7.x86_64 ( ...

随机推荐

  1. selenium_page_object

    最简单的pageobject github地址:https://github.com/defnngj/selenium_page_objects

  2. 二、myeclipse中配置mybatis中xml的自动提示

    以mybatis中mapper.xml为例 方法一: 步骤一:在mybatis-3.3.0.jar包中寻找mybatis-3-mapper.dtd文件. 可以用360压缩打开mybatis-3.3.0 ...

  3. CF-832B

    B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. c语言编译器内置宏

    注:转自http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html  感谢分享 前言: 我们在写程序的时候,总是 ...

  5. 09.客户端集成IdentityServer

    09.客户端集成IdentityServer 新建API的项目 dotnet new webapi --name ClientCredentialApi 在我们上一节课的代码IdentityServe ...

  6. Eclipse中实现JS代码提示功能

    转发: 用Eclipse写js代码时没有提示,很烦,心累: 找个各种方法以及插件,试了一下,个人感觉AngularJS Eclipse 插件很强,好用,不多说,先装上: 然后重启Eclipse ,右键 ...

  7. LeetCode: 476 Number Complement(easy)

    题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...

  8. WeFlow 简单使用教程

    一.前言 WeFlow 是什么?一个高效.强大.跨平台的前端开发工作流工具.(官网定义),下载那些你们都知道,我就不一 一介绍了.下面我说一下简单使用: 二.使用教程 首先,我们使用 WeFlow 是 ...

  9. E20190226-hm

    shallow  adj. 浅的,肤浅的; 表面的,皮毛的; (水,器物等) 浅的; (呼吸) 浅的;  n. 浅处; 浅滩;

  10. Chrome开发者工具 debug 调试

    Chrome 的开发者工具分为 8 个大模块,每个模块及其主要功能为: Element 标签页: 用于查看和编辑当前页面中的 HTML 和 CSS 元素. Network 标签页:用于查看 HTTP ...