Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Can not set TenantId to 0 for IMustHaveTenant entities error #8908


User avatar
0
wizgod created

Greetings Programs!

When I execute this code: <br>

        var userIds = approverIds.Select(q => new Abp.UserIdentifier(2, q.Value)).ToArray();
        message = message.Replace("[Name]", user.Name)
            .Replace("[Surname]", user.Surname)
            .Replace("[EmployeeID]", user.EmployeeId);

        var data = new MessageNotificationData(message) {["url"] = url};

        await _notificationPublisher.PublishAsync(notificationName, data, null,(NotificationSeverity)severity, userIds);

<br> I am receiving the following error:

ERROR 2020-04-22 16:58:30,000 [8    ] Mvc.ExceptionHandling.AbpExceptionFilter - Can not set TenantId to 0 for IMustHaveTenant entities! Abp.AbpException: Can not set TenantId to 0 for IMustHaveTenant entities! at Abp.EntityFrameworkCore.AbpDbContext.CheckAndSetMustHaveTenantIdProperty(Object entityAsObj) at Abp.EntityFrameworkCore.AbpDbContext.ApplyAbpConceptsForAddedEntity(EntityEntry entry, Nullable`1 userId, EntityChangeReport changeReport) at Abp.EntityFrameworkCore.AbpDbContext.ApplyAbpConcepts(EntityEntry entry, Nullable`1 userId, EntityChangeReport changeReport) at Abp.EntityFrameworkCore.AbpDbContext.ApplyAbpConcepts() at Abp.EntityFrameworkCore.AbpDbContext.SaveChangesAsync(CancellationToken cancellationToken) at Abp.Zero.EntityFrameworkCore.AbpZeroCommonDbContext`3.SaveChangesAsync(CancellationToken cancellationToken) at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesInDbContextAsync(DbContext dbContext) at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesAsync() at Abp.Notifications.NotificationStore.InsertNotificationAsync(NotificationInfo notification) at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous(IInvocation invocation) at Abp.Notifications.NotificationPublisher.PublishAsync(String notificationName, NotificationData data, EntityIdentifier entityIdentifier, NotificationSeverity severity, UserIdentifier[] userIds, UserIdentifier[] excludedUserIds, Nullable`1[] tenantIds)

Thanks,

Wg


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi wizgod

    • What is your product version?
    • What is your abp version?

    It doesn't seem to be a problem caused by the PublishAsync method. Please share the full code.

    Have you operated an entity that implements the IMustHaveTenant interface in the context of the method?

  • User Avatar
    0
    wizgod created

    Hi @maliming,

    Version 8.5.0.

    Here is the full class; I create a new list of Abp.UserIdentifier(tenant, userId) and there are values for the parameters being passed to PublishAsync.

    Thanks,

    Wg

        public class UserNotificationAppService : WizgodIncAppServiceBase, IUserNotificationAppService
        {
            private readonly INotificationPublisher _notificationPublisher;
            private readonly IUserAppService _userAppService;
            private readonly ITimeSheetApproversAppService _timeSheetApproversAppService;
            public UserNotificationAppService(INotificationPublisher notificationPublisher,
                IUserAppService userAppService,
                ITimeSheetApproversAppService timeSheetApproversAppService)
            {
                _notificationPublisher = notificationPublisher;
                _userAppService = userAppService;
                _timeSheetApproversAppService = timeSheetApproversAppService;
            }
    
            [AbpAllowAnonymous]
            public virtual async Task SendNotificationToApprovers(long fromUserId, string url, string message, string notificationName, int severity)
            {
                var user = await _userAppService.GetUser(new EntityDto<long> {Id = fromUserId});
    
                if (!user.TimeSheetApproversId.HasValue) return;
    
                var timeSheetApprovers = 
                    await _timeSheetApproversAppService.GetTimeSheetApprovers(
                        new EntityDto {Id = user.TimeSheetApproversId.Value});
    
                var approverIds = new List<long?> { timeSheetApprovers.ApproverId };
    
                if (timeSheetApprovers.FirstBackupApproverId.HasValue) approverIds.Add(timeSheetApprovers.FirstBackupApproverId.Value);
                if (timeSheetApprovers.SecondBackupApproverId.HasValue) approverIds.Add(timeSheetApprovers.SecondBackupApproverId.Value);
    
                var userIds = approverIds.Select(q => new Abp.UserIdentifier(2, q.Value)).ToArray();
    
                message = message.Replace("[Name]", user.Name)
                    .Replace("[Surname]", user.Surname)
                    .Replace("[EmployeeID]", user.EmployeeId);
    
                var data = new MessageNotificationData(message) {["url"] = url};
    
                await _notificationPublisher.PublishAsync(notificationName, data, null,(NotificationSeverity)severity, userIds);
            }
    
            [AbpAllowAnonymous]
            public virtual async Task SendNotificationToUser(long fromUserId, string url, string message, string notificationName, int notificationServerity, long userId)
            {
                await SendNotificationToUsers(fromUserId, url, message, notificationName, notificationServerity, new[] { userId });
            }
    
            [AbpAllowAnonymous]
            public virtual async Task SendNotificationToUsers(long fromUserId, string url, string message, string notificationName, int severity, long[] userIds)
            {
                var user = await UserManager.GetUserByIdAsync(fromUserId);
    
                var userIdentifiers = userIds.Select(q => new Abp.UserIdentifier(2, q)).ToArray();
    
                message = message.Replace("[Name]", user.Name)
                    .Replace("[Surname]", user.Surname)
                    .Replace("[EmployeeID]", user.EmployeeId);
    
                var data = new MessageNotificationData(message) {["url"] = url};
    
                await _notificationPublisher.PublishAsync(notificationName, data, severity: (NotificationSeverity)severity, userIds: userIdentifiers);
            }
        }
    
    
  • User Avatar
    0
    maliming created
    Support Team

    hi

    Your UserNotificationAppService looks fine.

    Are there any entities in your application that implement the IMustHaveTenant interface?

    Do you call its method directly in the web? Or call it in the controller, I think the possible reason is that there are other operations before calling its method.

    You can try to comment out all the methods in UserNotificationAppService to see if the error will continue to appear.

  • User Avatar
    0
    wizgod created

    Hi @maliming,

    I was updating entites (that implemented IMustHaveTenant) prior to sending the notification and I found that I needed to call CurrentUnitOfWork.SaveChangesAsync() before sending the notification.

    Thanks,

    Wg

  • User Avatar
    0
    maliming created
    Support Team

    hi

    Your problem has been solved, right?

  • User Avatar
    0
    wizgod created

    Hi @maliming,

    Yes, thanks,

    Wg