public async Task <LookupCode> Update(LookupCode lookupCode)
        {
            lookupCode.SortOrderForLocation.ThrowBusinessExceptionIfNull("SortOrderForLocation cannot be null");
            var savedLookup = await Db.LookupCode.Include(lc => lc.SortOrder.Where(so =>
                                                                                   so.LookupCodeId == lookupCode.Id &&
                                                                                   so.LocationId == lookupCode.SortOrderForLocation.LocationId))
                              .FirstOrDefaultAsync(lc => lc.Id == lookupCode.Id);

            savedLookup.ThrowBusinessExceptionIfNull($"Couldn't find lookup code with id: {lookupCode.Id}");

            if ((savedLookup.Code != lookupCode.Code ||
                 savedLookup.LocationId != lookupCode.LocationId)
                &&
                Db.LookupCode.AsNoTracking()
                .Any(lc => lc.Code == lookupCode.Code && lc.LocationId == lookupCode.LocationId))
            {
                throw new BusinessLayerException($"Attempted to create a duplicate entry.");
            }

            savedLookup.Location = await Db.Location.FindAsync(lookupCode.LocationId);

            Db.Entry(savedLookup).CurrentValues.SetValues(lookupCode);
            Db.Entry(savedLookup).Property(x => x.ExpiryDate).IsModified = false;

            var sortOrder = savedLookup.SortOrder.FirstOrDefault();

            if (sortOrder != null)
            {
                sortOrder.SortOrder = lookupCode.SortOrderForLocation.SortOrder;
                await Db.SaveChangesAsync();
            }
            else
            {
                var lookupSortOrder = new LookupSortOrder
                {
                    LookupCodeId = savedLookup.Id,
                    LocationId   = lookupCode.SortOrderForLocation.LocationId,
                    SortOrder    = lookupCode.SortOrderForLocation.SortOrder
                };
                await Db.LookupSortOrder.AddAsync(lookupSortOrder);

                await Db.SaveChangesAsync();

                lookupCode.SortOrderForLocation = lookupSortOrder;
            }
            return(lookupCode);
        }
        public async Task <LookupCode> Add(AddLookupCodeDto addLookupCode)
        {
            var lookupCd = addLookupCode.Adapt <LookupCode>();

            using TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
            lookupCd.Location            = await Db.Location.FindAsync(addLookupCode.LocationId);

            lookupCd.SortOrder = null;

            if (Db.LookupCode.AsNoTracking()
                .Any(lc => lc.Code == addLookupCode.Code && lc.LocationId == addLookupCode.LocationId))
            {
                throw new BusinessLayerException("Attempted to create a duplicate entry.");
            }

            await Db.LookupCode.AddAsync(lookupCd);

            await Db.SaveChangesAsync();

            if (addLookupCode.SortOrderForLocation != null)
            {
                var lookupSortOrder = new LookupSortOrder
                {
                    LookupCodeId = lookupCd.Id,
                    LocationId   = addLookupCode.SortOrderForLocation.LocationId,
                    SortOrder    = addLookupCode.SortOrderForLocation.SortOrder
                };
                await Db.LookupSortOrder.AddAsync(lookupSortOrder);

                await Db.SaveChangesAsync();

                lookupCd.SortOrderForLocation = lookupSortOrder;
            }
            scope.Complete();
            return(lookupCd);
        }