当前位置:网站首页>关于SqlSugar的多对多的级联插入的问题(无法获取集合属性的id,导致无法维护中间表)
关于SqlSugar的多对多的级联插入的问题(无法获取集合属性的id,导致无法维护中间表)
2022-06-29 06:41:00 【罗马苏丹默罕默德】
发现问题:
今天没有事,想着用.Net Core WebApi写一个RBAC的小Demo,
Demo很简单,主要就是Role和Permission,User和Role的两个多对多的关系,也就5张表的事。
打算使用ORM来简化操作,故使用SqlSugar来帮助开发。但在级联插入时好像出现了一些问题。
现有模型,和生成的表如下
[SugarTable(tableName:"t_permisssion")]
public class Permission
{
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
public long Id {
get; set; }
[SugarColumn(SerializeDateTimeFormat = "yyyy-mm-dd hh:mm:ss")]
public DateTime Create_Time {
get; set; }
public string Name {
get; set; }
public string Desc {
get; set; }
public string Type {
get; set; }
public string Create_Emp {
get; set; }
[Navigate(typeof(Role_Permisssion),nameof(Role_Permisssion.Permission_Id),nameof(Role_Permisssion.Role_Id))]
public List<Role> roles {
get; set; }
}
[SugarTable(tableName:"t_roles")]
public class Role
{
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
public long Id {
get; set; }
public string Name {
get; set; }
public string Desc {
get; set; }
[SugarColumn(SerializeDateTimeFormat = "yyyy-mm-dd hh:mm:ss")]
public string Create_Time {
get; set; }
public string Create_Emp {
get;set;}
[Navigate(typeof(Role_Permisssion),nameof(Role_Permisssion.Role_Id),nameof(Role_Permisssion.Permission_Id))]
public List<Permission> permissions {
get; set; }
}
[SugarTable(tableName:"t_role_permission")]
public class Role_Permisssion
{
[SugarColumn(IsPrimaryKey =true)]
public long Role_Id {
get; set; }
[SugarColumn(IsPrimaryKey = true)]
public long Permission_Id {
get; set; }
}

我们写个方法测试一下
[HttpPost]
public StandResult<object> Create_Role([FromBody]Role role)
{
SqlSugar.ISubInsertable<Role> res = scoper.GetTool().ScopedContext.Insertable(role).AddSubList(it => it.permissions);
int count = (int)res.ExecuteCommand();
//SqlSugar好像不会去自动维护中间表,所以要自己来获取自增ID来维护中间表
List<Role_Permisssion> list = new List<Role_Permisssion>();
role.permissions.ForEach(item =>
{
//在测试中Role.Id可以正确获取生成的自增ID,而其中的Permission集合的自增ID却无法获取
list.Add(new Role_Permisssion() {
Role_Id = role.Id, Permission_Id = item.Id });
});
scoper.GetTool().Insertable(list).ExecuteCommand();
if (count >= 1)
return new StandResult<object>() {
data = count,Status = 1,Message = "插入成功"};
return new StandResult<object>() {
data = count, Status = 0, Message = "插入失败" };
}
在swagger中测试插入,(这里的参数id是没有用的,它们实际上生成的是自增值)

这一点还是有点问题的,一般的ORM应该自动去维护中间表。故多对多的级联操作还是很重要的,毕竟可以简化开发过程。
目前的替代方法
当然,上面的问题是可以规避的,不过是要把对象的插入拆分开来,分别来获取其自增值
代码如下:
public StandResult<object> Create_Role([FromBody]Role role)
{
var res = scoper.GetTool().ScopedContext.Insertable(role);
//先插入Role,拿到自增Id
long count = res.ExecuteReturnBigIdentity();
List<Role_Permisssion> list = new List<Role_Permisssion>();
//再插入role中的permission集合,循环插入来获取自增值,并维护中间类的集合
role.permissions.ForEach(item => {
var res2 = scoper.GetTool().ScopedContext.Insertable(item).ExecuteReturnBigIdentity();
list.Add(new Role_Permisssion() {
Role_Id = count, Permission_Id = res2 });
});
//最后再插入中间类的集合
scoper.GetTool().Insertable(list).ExecuteCommand();
if (count >= 1)
return new StandResult<object>() {
data = count,Status = 1,Message = "插入成功"};
return new StandResult<object>() {
data = count, Status = 0, Message = "插入失败" };
}
效果如下:
总之,希望更新级联插入,自动维护中间表。
边栏推荐
- Cv:: mat and Base64 conversion (including picture compression and decompression)
- Postman pre request
- ES中配置ext.dic文件不生效的原因
- VSLAM特征之线特征&面特征
- C实战——高配版贪吃蛇游戏设计
- Explanation of swing transformer theory
- SQL injection bypass (6)
- 【工控老马】洗衣机PLC程序控制系统设计详解
- PostgreSQL安装:The database cluster initialisation failed,stack Hbulider安装
- ROS2中的行为树 BehaviorTree
猜你喜欢

Matlab Simulink simulation and analysis of power grid sweep frequency

AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放

手把手系列---安装SpotBugs、并快速上手使用

Wechat applet learning notes (summer vacation)

ROS2中的行为树 BehaviorTree

MongoDB-使用mongo/mongosh命令行连接数据库
![[industrial control old horse] detailed explanation of the design scheme of the running lamp control system based on Siemens S7-200PLC](/img/c5/9383a02050c83b26bb56f4045b0774.png)
[industrial control old horse] detailed explanation of the design scheme of the running lamp control system based on Siemens S7-200PLC

Roblox剑九之剑二

ES中配置ext.dic文件不生效的原因

Using cdockablepane to realize floating window in MFC
随机推荐
jsp学习部分
358. K distance interval rearrange string sorting
Detailed design of PLC program control system for washing machine
code::blocks代码格式化快捷键
【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解
ES中配置ext.dic文件不生效的原因
Swin Transformer理论讲解
pycharm的虚拟环境如何共享到jupyter-lab
Qtcreator set character set
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
Matlab Simulink simulation and analysis of power grid sweep frequency
AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation
C#导入csv到mysql数据库中
Detailed explanation of top and free commands
C编译器 - 隐式函数声明
TF. Repeat and stack operations of slim
Environmental preparation - Engineering Management
MIPS instruction set and brief analysis
SQL 注入绕过(六)
Little white battle pointer (Part 1)