【数据库-MySQL on Azure】如何使用 MySQL EntityFramework 组件处理 MYSQL PaaS DB
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 入门教程
【数据库-MySQL on Azure】如何使用 MySQL EntityFramework 组件处理 MYSQL PaaS DB的更多相关文章
- 如何使用 MySQL EntityFramework 组件处理 MYSQL PaaS DB
MySQL Database on Azure 是 Azure 平台上推出的 MySQL 云数据库服务,通过全面兼容 MySQL 协议,为用户提供了一个全托管的性能稳定.可快速部署.高可用.高安全性的 ...
- 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 ...
- mysql启动停止,一台服务器跑 多个mysql数据库
一.以非特权用户运行MySQL服务器在讨论如何启动MySQL服务器之前,让我们考虑一下应该以什么用户身份运行MySQL服务器.服务器可以手动或自动启动.如果你手动启动它, 服务器以你登录Unix(Li ...
- 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 ...
- Mysql 利用拷贝data目录文件的方式迁移mysql数据库
Mysql 利用拷贝data目录文件的方式迁移mysql数据库 步骤如下: 1.首先要确定data目录 这个问题困扰了我很久,因为网上的帖子大部分只是说拷贝mysql数据库目录下的data文件夹中的数 ...
- mysql分享一:运维角度浅谈MySQL数据库优化
转于:http://lizhenliang.blog.51cto.com/7876557/1657465 1.数据库表设计要合理避免慢查询.低效的查询语句.没有适当建立索引.数据库堵塞(死锁)等 2. ...
- Linux下XAMPP装完之后,Navicat无法连上数据库的问题的解决 注意'mypassword'是当前的mysql登录密码
Linux下装完XAMPP之后,mysql是自带装好了的,这个时候,mysql的root用户没有密码. 在mac 下安装好xampp后,需要在终端命令行操作时,比如输入:mysql -u root - ...
- 第六阶段·数据库MySQL及NoSQL实践第1章·章节一MySQL数据库
01 课程介绍 02 数据库管理系统介绍 03 MySQL安装方式介绍及源码安装 04 MySQL安装后的基本配置 05 MySQL体系结构-服务器.客户端模型 06 MySQL体系结构-实例.连接层 ...
- 【数据库】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 ( ...
随机推荐
- 第一行代码笔记的思维导图(http://images2015.cnblogs.com/blog/1089110/201704/1089110-20170413160323298-819915364.png)
- World is Exploding
题意: 给出一个长为n的序列A,问有多少四元组(a, b, c, d)满足$a \ne b \ne c \ne d, 1 \leq a < b \leq n, 1 \leq c < d \ ...
- jQuery.validator.addMethod方法的使用
该方法有三个api接口参数,name,method,messages addMethod(name,method,message)方法 参数 name 是添加的方法的名字. 参数 method 是一个 ...
- JSP提交表单的几种方法
1.通过<form action="url"><input type="submit"></form>按钮方式提交 这种方式 ...
- codeforces 813C The Tag Game 树+dfs追击问题
C. The Tag Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutpu ...
- LeetCode: 448 Find All Numbers Disappeared in an Array(easy)
题目: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice an ...
- 【废弃】JavaScript 删除部分
创建: 2019/02/26 添加: 2019/02/06 添加Object部分 添加: 2019/03/09 添加Function部分 毕竟自己曾经写的,彻底删得不留痕迹还是舍不得的. 但是的确已经 ...
- hdoj5289【RMQ+二分】【未完待续】
思路: 对当前值查找最近满足位置: 利用RMQ求出区间最大最小值,再枚举右端点,二分区间找到满足要求的最大区间累加
- 小程序地区时间自定义选择器 picker
进入微信公众平台小程序开发文档搜索 picker 点进去后下滑,点击在开发者工具中预览即可
- mongodb c# 序列化时 , Id引起的问题
1. c# 序列化时,如果没有指名_id , 如果class,struct有MemberName为 Id ,_id , 则自动识别为Id . 如果此时,这个"Id"是只读属性,就 ...