一、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. java多线程快速入门(二)

    通过继承Thread类来实行多线程 package com.cppdy; //通过继承Thread类来实行多线程 class MyThread extends Thread{ @Override pu ...

  2. 【ES】学习3-请求体查询

    1.空查询 GET /index_2014*/type1,type2/_search {} GET /_search { , } 2.查询表达式 DSL只需将查询语句传递给 query 参数 GET ...

  3. hdu3193 降维+rmq

    /* 给定n个数对(p,d),如果有这么一个数对(pi,di),其他所有的数对都不能严格小于这个数对 请求出有多少个这样的数对! 解法:对于数对(p,d),能找到在[1,p-1]之间的小于d的数对,那 ...

  4. Intellij IDEA配置tomcat热部署

    idea2017+tomcat8为本文的实验环境 1.打开tomcat的edit configuration,一定要选择war exploded  在idea tomcat 中server的配置里,有 ...

  5. ERP采购业务(三十七)

    产品构建表的添加存储过程: CREATE PROCEDURE [dbo].[BioPurchaseAppInfo_ADD] @PurchaseID INT OUTPUT, @Subject NVARC ...

  6. 【C++ Primer 第7章】定义抽象数据类型

    参考资料 1. C++Primer #7 类 Sales_data类 Sales_data.h #include<iostream> #include<string> clas ...

  7. android app 流量统计

    https://blog.csdn.net/yzy9508/article/details/48300265 | android 数据流量统计 - CSDN博客https://blog.csdn.ne ...

  8. [转] JavaScript 运行机制详解:再谈Event Loop

    一.为什么JavaScript是单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事.那么,为什么JavaScript不能有多个线程呢?这样能提高效率啊. Java ...

  9. java集合进行排序的两种方式

    java集合的工具类Collections中提供了两种排序的方法,分别是: Collections.sort(List list) Collections.sort(List list,Compara ...

  10. #13【BZOJ2794】[Poi2012]Cloakroom

    题解: 感觉真是很智障..连这么简单的题都没想出来 一直在想这么做动态背包..发现不会 首先显然我们将询问按照m 序列按照a[i]排序 然后怎么满足b呢 其实很简单啊..只需要记录f[i]表示前面这些 ...