0

Why is Entity Framework 5.0 inserting duplicate records in the Database in uncertain behavior when using dbcontext.AddAsync() then dbcontext.SaveChanges() method for adding data.

I have checked the existing value from records before inserting but still occurs duplication of value in the Database.

if (response.Item1.PurchasesItem.Any())
{
    foreach (var rec in response.Item1.PurchasesItem)
    {
        try
        {
            var exist = await _cFCDbContext.PurchaseSaleLog.Where(a => a.IsActive && a.ProductCode == rec.ItemId.Trim() && a.DaxDate.Equals(rec.PurchDate) && !a.IsDeleted).OrderByDescending(a => a.Id).FirstOrDefaultAsync();
            if (exist == null)
            {
                var purchase = new PurchaseSaleLog()
                {
                    ProductCode = rec.ItemId,
                    VendorId = rec.VendorId,
                    Type = ServiceType,
                    PUOM = rec.UOM,
                    Quantity = (rec.PurchQty.Equals("")) ? 0 : Convert.ToInt32(rec.PurchQty),
                    Branch = rec.Branch,
                    BranchStock = (rec.BranchStock.Equals("")) ? 0 : Convert.ToInt32(rec.BranchStock),
                    DaxDate = rec.PurchDate,
                    IsActive = true,
                    IsDeleted = false,
                    CreatedBy = userId,
                    CreatedDate = DateTime.UtcNow,
                    ModifiedBy = userId,
                    ModifiedDate = DateTime.UtcNow
                };
                await _cFCDbContext.PurchaseSaleLog.AddAsync(purchase);
                await _cFCDbContext.SaveChangesAsync().ConfigureAwait(false);
            }
            else
            {
                exist.ProductCode = rec.ItemId;
                exist.VendorId = rec.VendorId;
                exist.Type = ServiceType;
                exist.Quantity = (rec.PurchQty.Equals("")) ? 0 : Convert.ToInt32(rec.PurchQty);
                exist.Branch = rec.Branch;
                exist.PUOM = rec.UOM;
                exist.BranchStock = (rec.BranchStock.Equals("")) ? 0 : Convert.ToInt32(rec.BranchStock);
                exist.DaxDate = rec.PurchDate;
                exist.ModifiedBy = userId;
                exist.ModifiedDate = DateTime.UtcNow;
                _cFCDbContext.PurchaseSaleLog.Update(exist);
                await _cFCDbContext.SaveChangesAsync().ConfigureAwait(false);
            }

        }
        catch (Exception ex)
        {
            continue;
        }
    }
}
Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • 2
    Dont shallow your exceptions, in the `catch` block you should log and/or alter of the problem. – Cleptus Jun 24 '22 at 06:15
  • Can you share the model class that you are trying to add and your configuration of EF – srzsanti Jun 24 '22 at 14:44
  • public class PurchaseSaleLog:CommonEntity{ [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] public string ProductCode { get; set; } [Required] [StringLength(100)] public string VendorId { get; set; } public string Type { get; set; } public int Quantity { get; set; } public string PUOM { get; set; } public string Branch { get; set; } public int BranchStock { get; set; } public DateTime DaxDate { get; set; } } – Confiz Waqar Jun 27 '22 at 10:23

0 Answers0