C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 主细表事务处理的标准例子
2023-02-18 15:32:08 时间
要么就一次全成功,要么就一次全失败,插入主表的同时插入关联的子表,主表插入时返回当前插入的主键,字表插入时用这个主键做关联。
参考代码如下:
/// <summary> /// 添加角色(同时添加用户,一个数据库事务里进行处理) /// </summary> /// <param name="userInfo">用户</param> /// <param name="entity">实体</param> /// <param name="userIds">用户主键数组</param> /// <param name="statusCode">返回状态码</param> /// <param name="statusMessage">返回状态信息</param> /// <returns>主键</returns> public string AddWithUser(BaseUserInfo userInfo, BaseRoleEntity entity, string[] userIds, out string statusCode, out string statusMessage) { var parameter = ServiceParameter.CreateWithMessage(userInfo , MethodBase.GetCurrentMethod() , this.serviceName , AppMessage.RoleService_Add); string result = string.Empty; string returnCode = string.Empty; string returnMessage = string.Empty; ServiceUtil.ProcessUserCenterWriteDbWithTransaction(userInfo, parameter, (dbHelper) => { // 这里是判断已经登录的用户是否有调用当前函数的权限,加强服务层被远程调用的安全性的 var permissionManager = new BasePermissionManager(userInfo); if (permissionManager.CheckPermissionByUser(userInfo.Id, "RoleAdmin.Add", "添加角色")) { string tableName = userInfo.SystemCode + "Role"; var manager = new BaseRoleManager(dbHelper, userInfo, tableName); result = manager.Add(entity, out returnCode); if (!string.IsNullOrEmpty(result) && userIds != null && userIds.Length > 0) { tableName = userInfo.SystemCode + "UserRole"; var userManager = new BaseUserManager(dbHelper, userInfo, tableName); // 小心异常,检查一下参数的有效性 if (userIds != null) { userManager.AddToRole(userIds, result); } } returnMessage = manager.GetStateMessage(returnCode); } }); statusCode = returnCode; statusMessage = returnMessage; return result; }
相关文章
- React报错之Rendered more hooks than during the previous render
- 如何使用CSS伪类选择器
- React报错之Property 'value' does not exist on type EventTarget
- React报错之Parameter 'event' implicitly has an 'any' type
- React报错之Parameter 'props' implicitly has an 'any' type
- React报错之Property 'value' does not exist on type 'HTMLElement'
- React报错之You provided a `checked` prop to a form field
- React报错之Invalid hook call
- React报错之React hook 'useState' cannot be called in a class component
- React报错之React Hook 'useEffect' is called in function
- React报错之React hook 'useState' is called conditionally
- 如何在CSS中使用变量
- React报错之React Hook useEffect has a missing dependency
- React报错之Expected an assignment or function call and instead saw an expression
- React报错之Unexpected default export of anonymous function
- React报错之Expected `onClick` listener to be a function
- React报错之Cannot find namespace context
- React报错之Functions are not valid as a React child
- React报错之Encountered two children with the same key
- React报错之Cannot assign to 'current' because it is a read-only property