예제 #1
0
        /// <inheritdoc />
        public virtual async Task AddToToManyRelationshipAsync(TId primaryId, ISet <IIdentifiable> secondaryResourceIds, CancellationToken cancellationToken)
        {
            _traceWriter.LogMethodStart(new { primaryId, secondaryResourceIds });
            if (secondaryResourceIds == null)
            {
                throw new ArgumentNullException(nameof(secondaryResourceIds));
            }

            var relationship = _targetedFields.Relationships.Single();

            if (secondaryResourceIds.Any())
            {
                using var collector = new PlaceholderResourceCollector(_resourceFactory, _dbContext);
                var primaryResource = collector.CreateForId <TResource, TId>(primaryId);

                await UpdateRelationshipAsync(relationship, primaryResource, secondaryResourceIds, collector, cancellationToken);

                await SaveChangesAsync(cancellationToken);
            }
        }
        /// <inheritdoc />
        public virtual async Task DeleteAsync(TId id, CancellationToken cancellationToken)
        {
            _traceWriter.LogMethodStart(new { id });

            using var collector = new PlaceholderResourceCollector(_resourceFactory, _dbContext);
            var resource = collector.CreateForId <TResource, TId>(id);

            foreach (var relationship in _resourceGraph.GetRelationships <TResource>())
            {
                // Loads the data of the relationship, if in EF Core it is configured in such a way that loading the related
                // entities into memory is required for successfully executing the selected deletion behavior.
                if (RequiresLoadOfRelationshipForDeletion(relationship))
                {
                    var navigation = GetNavigationEntry(resource, relationship);
                    await navigation.LoadAsync(cancellationToken);
                }
            }

            _dbContext.Remove(resource);

            await SaveChangesAsync(cancellationToken);
        }