一、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. 使用navicat premium将数据库从Oracle迁移到SQL Server,或从Oracle迁移到MySQL

    有时候我们有迁移数据库的需求,例如从Oracle迁移到SQL Server,或者从MySQL迁移到Oracle. 很多江湖好汉一时不知如何手工操作,所幸的是Navicat提供了迁移的自动化操作界面. ...

  2. Coursera台大机器学习技法课程笔记07-Blending and Bagging

    这一节讲如何将得到的feature或hypothesis组合起来用于预测. 1. 林老师给出了几种方法 在选择g时,需要选择一个很强的g来确保Eval最小,但如果每个g都很弱该怎么办呢 这个时候可以选 ...

  3. php中类继承和接口继承的对比

    PHP类继承: 1.PHP类不支持多继承,也就是子类只能继承一个父类,但是支持多层次继承,比如: class frist{ public function __construct(){ echo &q ...

  4. jquery----data使用

    - .data() - .data("key", value) 保存值,value可以是字符串,也可以是数组,也可以是jquery对象- .data("key" ...

  5. MySQL5.7的新特性

    MySQL 5.7版本据说已经在了很大的性能提升以及做得更加安全了,想了解更多MySQL 5.7的新特性可以参考我转载叶金荣老师的MySQL 5.7的新特性说明.这里我简单演示一下MySQL 5.7的 ...

  6. hdu 2112 map+Dijkstra

    无向图 用map 起点和终点可能一样 数组不能开太大 WA了好多发 Sample Input6xiasha westlake //起点 终点xiasha station 60xiasha Shoppi ...

  7. mybatis的sqlSessionFactory的加载过程

    使用过SSM的框架的都知道mybatis这个持久层框架,今天小编就来简单说说这个框架的核心工厂类sqlSessionFactory的加载过程,一般的SSM框架我们都会在spring的applicati ...

  8. ModuleNotFoundError: No module named 'win32api'

    原因:缺少了win32解决: pip install pypiwin32

  9. pygame游戏开发入门例子

    # *_* coding:utf-8 *_* # 开发团队:中国软件开发团队# 开发人员:Administrator# 开发时间:2019/3/23 11:16# 文件名称:pygame_demo# ...

  10. python tkinter-单选、多选

      单选按钮 tkinter.Radiobutton(root,text='a').pack() tkinter.Radiobutton(root,text='b').pack() tkinter.R ...