EF Core 5.0插入重复记录

发布于 2025-02-10 00:37:32 字数 2419 浏览 1 评论 0原文

为什么使用dbcontext.addasync()然后dbcontext.savechanges()方法来添加数据时,为什么实体框架5.0在数据库中以不确定的行为插入重复记录。

在插入之前,我已经从记录中检查了现有值,但仍会在数据库中重复。

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;
        }
    }
}

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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文