protected virtual List <UserNotification> SaveUserNotifications(UserIdentifier[] users, NotificationInfo notificationInfo) { var userNotifications = new List <UserNotification>(); var tenantGroups = users.GroupBy(user => user.TenantId); foreach (var tenantGroup in tenantGroups) { using (_unitOfWorkManager.Current.SetTenantId(tenantGroup.Key)) { var tenantNotificationInfo = new TenantNotificationInfo(_guidGenerator.Create(), tenantGroup.Key, notificationInfo); _notificationStore.InsertTenantNotification(tenantNotificationInfo); _unitOfWorkManager.Current.SaveChanges(); //To get tenantNotification.Id. var tenantNotification = tenantNotificationInfo.ToTenantNotification(); foreach (var user in tenantGroup) { var userNotification = new UserNotificationInfo(_guidGenerator.Create()) { TenantId = tenantGroup.Key, UserId = user.UserId, TenantNotificationId = tenantNotificationInfo.Id }; _notificationStore.InsertUserNotification(userNotification); userNotifications.Add(userNotification.ToUserNotification(tenantNotification)); } CurrentUnitOfWork.SaveChanges(); //To get Ids of the notifications } } return(userNotifications); }