乐观并发异常 - 仅在服务器上

问题描述

我的 WebAPI 在 localhost 中成功运行(使用 Angular 中的 UI 和邮递员)但是当我将它发布到 Azure 服务器时,它在尝试更新现有实体时开始抛出乐观并发异常。即使我使用本地 Visual Studio 2019 连接服务器 DB 并通过邮递员进行测试,也会出现相同的异常。

自过去 3-4 周以来,这个问题让我发疯。我已经确认每个属性都正确绑定,包括外键。而Server数据库的表结构与localhost相同。

我正在使用具有存储库模式的实体框架 6、.Net 框架 4.8、SQL Server 2017(尝试使用 SQL Server 2014)。

我试图解决问题但没有成功:

  • 将 [ConcurencyCheck] 设置为属性
  • 确保没有其他线程正在尝试更改实体或没有其他事务正在运行
  • 禁用外键并只更新表的单列
  • 我可以在同一个表中插入新记录
  • 将数据库和 API 都发布到不同的服务器
  • 尝试使用 Client wins (https://docs.microsoft.com/en-gb/ef/ef6/saving/concurrency?redirectedfrom=MSDN) 刷新实体并再次保存更改

我现在不知道要解决这个问题。 有没有人面临同样的问题?或者我缺少的任何配置还是应该尝试?

请不要将此问题标记为重复,因为我尝试了在不同线程中找到的所有可能的解决方案。

[编辑] 示例代码:

// LeadCustomer is inheriting from baseclass ModelBase which has CreatedById,CreatedDate,ModifiedById,ModifiedDate properties
LeadCustomer leadCustomer = dc.LeadCustomers.Where(c => c.Email.EmailAddress.ToLower().Equals(model.EmailAddress.ToLower())).FirstOrDefault();
    if (leadCustomer == null) { 
        leadCustomer.FirstName = "Viral";
        leadCustomer.LastName = "Maru";
        leadCustomer.Email.EmailAddress = "maru.viral@gmail.com"; 
        leadCustomer.SubscriptionTypeID = SubscriptionTypes.Monthly; 
        dc.LeadCustomers.Add(leadCustomer); 
        dc.SaveChanges(); 
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...