当前位置:网站首页>About the many to many cascading insertion of sqlsugar (the ID of the collection attribute cannot be obtained, so the intermediate table cannot be maintained)
About the many to many cascading insertion of sqlsugar (the ID of the collection attribute cannot be obtained, so the intermediate table cannot be maintained)
2022-06-29 08:05:00 【Roman Sultan Mohammed】
Find the problem :
I have nothing to do today , Think of using .Net Core WebApi Write a RBAC Small Demo,
Demo It's simple , Mainly Role and Permission,User and Role Two many to many relationships , It's just 5 About the watch .
Intended use ORM To simplify operations , Therefore, using SqlSugar To help develop . But there seems to be some problems with cascading inserts .
Existing models , And the generated table is as follows
[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; }
}

Let's write a method to test
[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 I don't seem to be able to maintain the intermediate table automatically , Therefore, we should gain self growth by ourselves ID To maintain intermediate tables
List<Role_Permisssion> list = new List<Role_Permisssion>();
role.permissions.ForEach(item =>
{
// In the test Role.Id The generated auto increment can be obtained correctly ID, And one of the Permission Autoincrement of set ID But I can't get
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 = " Insert the success "};
return new StandResult<object>() {
data = count, Status = 0, Message = " Insert the failure " };
}
stay swagger Test insert ,( Parameters here id It's no use , What they actually generate is self increment )

There is still a problem with this , General ORM The intermediate table should be maintained automatically . Therefore, many to many cascading operation is still very important , After all, it can simplify the development process .
Current alternatives
Of course , The above problems can be avoided , It's just to split the insertion of objects , Respectively to obtain its self increment
The code is as follows :
public StandResult<object> Create_Role([FromBody]Role role)
{
var res = scoper.GetTool().ScopedContext.Insertable(role);
// First insert Role, Get self increasing Id
long count = res.ExecuteReturnBigIdentity();
List<Role_Permisssion> list = new List<Role_Permisssion>();
// Insert again role Medium permission aggregate , Loop insert to get self increment , And maintain a collection of intermediate classes
role.permissions.ForEach(item => {
var res2 = scoper.GetTool().ScopedContext.Insertable(item).ExecuteReturnBigIdentity();
list.Add(new Role_Permisssion() {
Role_Id = count, Permission_Id = res2 });
});
// Finally, insert the collection of intermediate classes
scoper.GetTool().Insertable(list).ExecuteCommand();
if (count >= 1)
return new StandResult<object>() {
data = count,Status = 1,Message = " Insert the success "};
return new StandResult<object>() {
data = count, Status = 0, Message = " Insert the failure " };
}
The effect is as follows :
All in all , Want to update cascading inserts , Automatically maintain intermediate tables .
边栏推荐
- VMware vcenter/ESXI系列漏洞总结
- MySQL enable logging
- C mqtt subscription message
- Cartographer中的线程池操作
- js:Array.reduce累加计算、合并数组
- 【深度之眼吴恩达机器学习作业班第四期】Linear Regression with One Variable,单变量线性回归
- JSP learning part
- Basics - syntax standards (ANSI C, ISO C, GNU C)
- JS XOR obfuscation code
- Sonic communication - streaming data processing - window alignment
猜你喜欢

Behaviortree in ros2

AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation

Code:: blocks code formatting shortcuts

Line features & surface features of vSLAM features

MySQL系统关键字汇总(官网)

Un voyage profond d'IA dans Huawei Cloud

SQL Server 开启cdc

Robotframework learning notes: introduction to robot framework and browserlibrary (playwright)

【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结
![[eye of depth Wu Enda's fourth operation class] summary of multiple linear regression with multiple variables](/img/51/581be1bdfe7cc97193ff68d3ec5d22.png)
[eye of depth Wu Enda's fourth operation class] summary of multiple linear regression with multiple variables
随机推荐
Simulation time and bag operation in ROS
Thread pool operations in cartographer
Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘
SVM, problems encountered in face recognition and Solutions
Protobuf binary file learning and parsing
Flutter 文件读写-path_provider
多态中的向上和向下转型
【6G】算力网络技术白皮书整理
MySQL系统关键字汇总(官网)
JSP learning part
Code:: blocks code formatting shortcuts
nor flash 应用层操作
[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming
Reflection perfectionism
Line features & surface features of vSLAM features
VMware vcenter/ESXI系列漏洞总结
关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知
华为云的AI深潜之旅
1031 Hello World for U
[eye of depth wuenda machine learning homework class phase IV] regularization regularization summary