一、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. 解决Linux安装 VMware tools 工具的方法

    一:启动linux服务器,并用远程登录工具访问linux服务器 1:启动系统 2:用服务器控制台   :查看点ip地址 3:用客户端 连接服务器 二:挂起 vm虚拟机的 tools 安装光盘 三:开始 ...

  2. 缓存之EHCache(一)

    源文: http://blog.csdn.net/l271640625/article/details/20528573 一.简介 非常简单,而且易用.     ehcache 是一个非常轻量级的缓存 ...

  3. 追求极致--纯css制作三角、圆形按钮,兼容ie6

    参考了天猫.微博等网站的做法,用纯html和css实现,效果还是不错的.以下是成果,兼容主流浏览器,包括ie6. <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  4. C 长字符串换行方法

    C中字符串有时候会出现很长的情况,如果不换行书写查看起来很不方便. 长字符串拆分成多行处理也是C规范的一部分. 方法1. 利用双引号" " ,将长字符串分成多个子串换行,C会自动无 ...

  5. impress.js

    介绍一下 impress.js是一个非常炫酷的幻灯片展示框架,依靠CSS3技术. impress.js使用起来非常简单,下面就来简单介绍一下其用法. Start 首先,当然要引入impress.js. ...

  6. LeetCode(35):搜索插入位置

    Easy! 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1 ...

  7. 遍历DOM树

    遍历DOM在jQuery中是非常重要的技术. 遍历DOM之前,需要对DOM有清晰的认识,了解文档节点.元素节点.属性节点.文本节点等相关概念.不清楚可以温习下<JavaScript教程.DOM树 ...

  8. VMware虚拟机 Ubuntu 16.04 安装

    第一步:VMware虚拟机 Ubuntu 16.04 安装 第二步: 解决窗口全屏问题 linux下给root用户设置密码 修改root用户的密码 $ sudo passwd root 密码会要求重复 ...

  9. Git强制更新本地库和冲突解决

    1.You have not concluded your merge. (MERGE_HEAD exists) 本地有修改和提交,如何强制用远程的库更新.出现这种情况一般是git本地有commit, ...

  10. Python contains

    一.__contains__ 判断字符串中是否包含相应的字符.