当前位置:网站首页>关于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 = "插入失败" };
}
效果如下:
总之,希望更新级联插入,自动维护中间表。
边栏推荐
- Using cdockablepane to realize floating window in MFC
- 道闸控制器通讯协议
- cv2.cvtColor
- Matlab Simulink simulation and analysis of power grid sweep frequency
- Problem solving -- > online OJ (13)
- Up and down transitions in polymorphism
- VMware vcenter/ESXI系列漏洞总结
- 互联网公司的组织结构与产品经理岗位职责是什么?
- AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation
- 线段树以及使用
猜你喜欢

Common MySQL errors and solutions summarized painstakingly (I)

VMware vcenter/ESXI系列漏洞总结
![Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200](/img/56/b300c0c3606dbc328e301092615bff.jpg)
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200

【修复收藏功能、更新登录接口】知识付费小程序、博客小程序、完整版开源源码、资源变现小程序,带299整站资源数据

Using cdockablepane to realize floating window in MFC

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?

【工控老马】单片机与西门子S7-200通信原理详解

呕心沥血总结出来的MySQL常见错误以及解决方法(一)

面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?

【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
随机推荐
精选西门子PLC工程实例源码【共300套】
打包时提示: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘
反思 - 中小公司项目管理思维 - 先将产品做出来,先将功能做出来
Pointer reference array element
C actual combat - high configuration version of Snake game design
js异或混淆代码
手把手系列---安装SpotBugs、并快速上手使用
Roblox剑九之剑二
[FreeRTOS] interrupt mechanism
Common MySQL errors and solutions summarized painstakingly (I)
SAP ui5 Beginner (I) Introduction
Summary of array knowledge points
100 lectures on Excel advanced drawing skills (VI) - practical application cases of Gantt chart in project progress
Line features & surface features of vSLAM features
Cv:: mat and Base64 conversion (including picture compression and decompression)
从Nacos客户端谈Nacos配置中心
【工控老马】洗衣机PLC程序控制系统设计详解
Soft restart
Embedded product anti-theft version
ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector