Skip to content

BulkRead erroring with " An item with the same key has already been added" on one-to-many relation #733

Closed
@Tyler-V

Description

@Tyler-V

Hi @borisdj

We are attempting to implement bulk services to read a parent table by Id, then read the child relation (one-to-many) by the joining/navigational property of the parent id, then assign those child properties to the parent to return the complete object.

The problem is the child relation, is a one-to-many and can be assigned to many of the parent Entity (Package)

System.Private.CoreLib: Exception while executing function: Package-Reorder. System.Private.CoreLib: An item with the same key has already been added. Key: 19.

This is the error we are encountering, is there a work-around for this other than querying for the ids of the child table with a where contains and then materializing the child query?

            var packageAssignmentIds = await _packageReservationContext.PackageAssignment
                .Where(pa => packageIds.Contains(pa.PackageId))
                .Select(pa => pa.Id)
                .ToListAsync();

This is what we are using that triggered the error,

        public async Task<List<Package>> GetPackagesAsync(List<int> packageIds)
        {
            var packages = packageIds.Select(id => new Package() { Id = id }).ToList();
            await _packageReservationContext.BulkReadAsync(packages, new BulkConfig
            {
                UpdateByProperties = new List<string> { nameof(Package.Id) },
                BatchSize = 10000
            });
            
            var packageAssignments = packageIds.Select(id => new PackageAssignment() { PackageId = id }).ToList();
            await _packageReservationContext.BulkReadAsync(packageAssignments, new BulkConfig // <----- ERROR
            {
                UpdateByProperties = new List<string> { nameof(PackageAssignment.PackageId) },
                BatchSize = 10000
            });

Entities below,

    public abstract class BaseEntity
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        [Column(Order = 0)]
        public int Id { get; set; }

        public string CreatedBy { get; set; }

        public string UpdatedBy { get; set; }

        public DateTimeOffset? DateCreated { get; set; }

        public DateTimeOffset? DateModified { get; set; }

        public string Notes { get; set; }
    }
    public class Package : BaseEntity
    {
        public int AccumulationGroupId { get; set; }
        public string NdcNumber { get; set; }
        public int? OrderDetailId { get; set; }
        public DateTimeOffset? ReplenishmentCompleteDate { get; set; }
        public OrderDetail OrderDetail { get; set; }
        public int? InvoiceDetailId { get; set; }
        public InvoiceDetail InvoiceDetail { get; set; }
        public int?  WholesalerAccountId { get; set; }
        public PackageStatus Status { get; set; } = PackageStatus.Open;
        public List<PackageAssignment> PackageAssignments { get; set; } = new List<PackageAssignment>();
    }
    public class PackageAssignment : BaseEntity
    {
        public int PackageId { get; set; }
        public Package Package { get; set; }
        public int AccumulationFactId { get; set; }
        public int CaptureHistoryId { get; set; }
        public decimal PercentOfPackage { get; set; }
        public decimal Quantity { get; set; }
    }

image

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions