一、Pomelo.EntityFrameworkCore.MySql简介

Git源代码地址:https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

注:这是第三方的 EF Core 的ORM框架,支持Synac I/O访问操作MySql数据,不是MySql官方的数据驱动。

1.本框架支持Code First 和Server First

问题:Server First 生成代码没有附带主外键关联。

二、Server First方式使用示例

1.创建.Net Core 控制台项目

2.安装Pomelo.EntityFrameworkCore.MySql

使用命令:

Install-Package Pomelo.EntityFrameworkCore.MySql

或者在包管理工具中搜索安装

创建数据库角色-菜单命令:

-- 创建角色&菜单简单逻辑表
create database RoleMenu;
grant all on *.* to 'userone'@'localhost' identified by ''; use RoleMenu;
-- drop table Role_Menu,Role,Menu;
-- 创建角色表
create table Role(
RoleID int not null auto_increment,
Name nvarchar(50) not null,
SortValue int not null,
primary key(RoleID)
); -- 创建菜单表
create table Menu(
MenuID int not null auto_increment,
Name nvarchar(50) not null,
Title nvarchar(100) not null,
LinkUrl varchar(200) null,
Icon varchar(100) null,
primary key(MenuID)
); -- 创建角色-菜单表
create table Role_Menu(
ID int not null auto_increment,
RoleID int not null,
MenuID int not null,
primary key(ID),
foreign key(RoleID) references Role(RoleID)
on delete cascade,
foreign key(MenuID) references Menu(MenuID)
on delete no action
); -- 添加测试数据
insert Role(Name,SortValue) values('系统管理员',1);
insert Role(Name,SortValue) values('服务中心',2); -- 添加菜单数据
insert Menu(Name,Title) values('个人信息','个人信息管理');
insert Menu(Name,Title) values('修改密码','修改登录密码&二级密码'); -- 添加关联
insert Role_Menu(RoleID,MenuID) values(1,1);
insert Role_Menu(RoleID,MenuID) values(1,2);
insert Role_Menu(RoleID,MenuID) values(2,1);
insert Role_Menu(RoleID,MenuID) values(2,2);

使用PM命令,链接数据库生成model层和上下文:

Scaffold-DbContext "Server=127.0.0.1;port=3306;Database=Md5Data;uid=xxx;pwd=xxx;Character Set=utf8;" MySql.Data.EntityFrameworkCore -OutputDir models2 

生成代码结果:

    public partial class Role
{
public int RoleId { get; set; }
public string Name { get; set; }
public int SortValue { get; set; }
}
public partial class Menu
{
public int MenuId { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string LinkUrl { get; set; }
public string Icon { get; set; }
}
public partial class RoleMenu
{
public int Id { get; set; }
public int RoleId { get; set; }
public int MenuId { get; set; }
}
public partial class RoleMenuContext : DbContext
{
public virtual DbSet<Menu> Menu { get; set; }
public virtual DbSet<Role> Role { get; set; }
public virtual DbSet<RoleMenu> RoleMenu { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseMySql("Server=127.0.0.1;port=3306;Database=RoleMenu;uid=userone;pwd=123;Character Set=utf8;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Menu>(entity =>
{
entity.Property(e => e.MenuId)
.HasColumnName("MenuID")
.HasColumnType("int(11)"); entity.Property(e => e.Icon).HasMaxLength(); entity.Property(e => e.LinkUrl).HasMaxLength(); entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(); entity.Property(e => e.Title)
.IsRequired()
.HasMaxLength();
}); modelBuilder.Entity<Role>(entity =>
{
entity.Property(e => e.RoleId)
.HasColumnName("RoleID")
.HasColumnType("int(11)"); entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(); entity.Property(e => e.SortValue).HasColumnType("int(11)");
}); modelBuilder.Entity<RoleMenu>(entity =>
{
entity.ToTable("Role_Menu"); entity.HasIndex(e => e.MenuId)
.HasName("MenuID"); entity.HasIndex(e => e.RoleId)
.HasName("RoleID"); entity.Property(e => e.Id)
.HasColumnName("ID")
.HasColumnType("int(11)"); entity.Property(e => e.MenuId)
.HasColumnName("MenuID")
.HasColumnType("int(11)"); entity.Property(e => e.RoleId)
.HasColumnName("RoleID")
.HasColumnType("int(11)");
});
}
}

更多:

.NetCore中EFCore for MySql整理(二)

.NetCore中EFCore for MySql整理

.NetCore中EFCore的使用整理

.NetCore中EFCore for MySql整理(三)之Pomelo.EntityFrameworkCore.MySql的更多相关文章

  1. .NetCore中EFCore的使用整理(三)-关联表操作

    一.查询关联表数据 StudyAboard_TestContext _context = new StudyAboard_TestContext(); CrmRole role = _context. ...

  2. .NetCore中EFCore的使用整理(二)-关联表查询

    EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core  1.1 1.当前的版本,还不支持延迟加载(Lazy ...

  3. .NetCore中EFCore的使用整理

    EntirtyFramework框架是一个轻量级的可扩展版本的流行实体框架数据访问技术. 其中的.NetCore版本对应EntityFrameworkCore Git源代码地址:https://git ...

  4. .NetCore中EFCore for MySql整理(二)

    一.简介 EF Core for MySql的官方版本MySql.Data.EntityFrameworkCore 目前正是版已经可用当前版本v6.10,对于以前的预览版参考:http://www.c ...

  5. An item with the same key has already been added. Key: Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal.MySqlOptionsExtension

    An item with the same key has already been added. Key: Pomelo.EntityFrameworkCore.MySql.Infrastructu ...

  6. .NetCore中EFCore for MySql整理

    一.MySql官方提供了Ef Core对MySql的支持,但现在还处于预览版 Install-Package MySql.Data.EntityFrameworkCore -Pre Install-P ...

  7. python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb

    在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...

  8. NetCore 中 EFcore的DbFirst和CodeFirst混合 使用注意

    NetCore 最近很火热.笔者想把自己以前的旧项目迁移到NetCore平台. 先用EFcore的DBFirst根据数据库创建实体类,然后加入数据库版本控制功能也就是EFcore的CodeFirst部 ...

  9. Asp.Net Core中Json序列化处理整理

    一.Asp.Net Core中的Json序列化处理使用的是Newtonsoft.Json,更多参考:C# Newtonsoft.Json JsonSerializerSettings配置序列化操作,C ...

随机推荐

  1. [转]centos7 下安装MongoDB

    查看MongoDB的最新版官方下载地址: https://www.mongodb.com/download-center#community 使用wget命令下载安装包 wget https://fa ...

  2. LeetCode(36): 有效的数独

    Medium! 题目描述: 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1 ...

  3. LeetCode(30):与所有单词相关联的字串

    Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  4. Laravel框架中的event事件操作

    有时候当我们单纯的看 Laravel 手册的时候会有一些疑惑,比如说系统服务下的授权和事件,这些功能服务的应用场景是什么,其实如果没有经历过一定的开发经验有这些疑惑是很正常的事情,但是当我们在工作中多 ...

  5. 目标检测-ssd

    intro: ECCV 2016 Oral arxiv: http://arxiv.org/abs/1512.02325 paper: http://www.cs.unc.edu/~wliu/pape ...

  6. 使用gunicorn将django项目部署到生产环境的子目录下,在nginx后端获取客户真实IP地址

    生产环境有时,并不是为了一个项目而存在的.毕竟,域名是比较稀有的. 今天遇到这个问题,解决了.作个记录. 并且,如果将django项目部署在Nginx后面,那如何获取用户真实的IP地址呢? 下面就来解 ...

  7. python常用内建模块--collections

    1.namedtuple #namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素.#这样一来,我们用n ...

  8. BZOJ5071 小A的数字 BZOJ2017年10月月赛 其他

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ5071 题意概括 题解 一开始蒙了. 感觉做过类似的题目. 但是找不到方法. 突然想到前缀和! 对于 ...

  9. 【Java】 剑指offer(4) 替换空格

    本文参考自<剑指offer>一书,代码采用Java语言.  更多:<剑指Offer>Java实现合集 题目 请实现一个函数,把字符串中的每个空格替换成"%20&quo ...

  10. Scrapy爬虫学习笔记 - 爬虫基础知识

    一.正则表达式 二.深度和广度优先                                三.爬虫去重策略