解决Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException的问题

时间:2019-09-12 09:41:17

标签: sql-server asp.net-core asp.net-web-api concurrency entity-framework-core

在使用Entity Framework Core对表进行插入操作时遇到问题。

_context.Entry(item).State = EntityState.Added;
var r = await _context.ServiceWorkOrders.AddAsync(item);
_context.SaveChangesAsync(); <-- (fails)

处理此问题时的某些情况。

  • 该表归客户所有,因此我必须解决此问题
  • 要插入的表,包含触发器和存储过程,这些触发器和存储过程设置为在执行插入/更新/删除操作之后运行。
  • 表具有关系属性(外键)
  • 表的主键设置为自动递增,因此插入实体的主键字段以及此插入过程中所需的字段都设置为0。

我试图直接通过ExecuteSqlCommand使用存储过程,但是我更喜欢使用EF来管理数据库访问。而且,如果我错了,请纠正我,我将不得不在存储过程中列出所有可选参数,以便在其中添加实体,以防止写入错误的字段。当前,此方法会插入实体,但是即使我使用了SqlParameters(“ @ named_field”,value),它也会在错误的字段上写入。

我也尝试过使用Synchronous方法,但是它给出了相同的例外。

异常返回:

  

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:'数据库操作预期会影响1行,但实际上影响0行。自加载实体以来,数据可能已被修改或删除。有关了解和处理乐观并发异常的信息,请参见http://go.microsoft.com/fwlink/?LinkId=527962

编辑:

这是实体模型:

[Table("ASM_ServiceWorkOrder")]
public class ServiceWorkOrder: BaseEntity
{

    [Key, Column(name: "ROWUID"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int RowUID { get; set; }

    [Column(TypeName = "nvarchar(25)")]
    public string CompanyID { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string DocNumber { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string RevisionNumber { get; set; }

    [Column(TypeName = "nvarchar(40)")]
    public string CustomerDirectoryID { get; set; }

    [ForeignKey("CustomerLocation")]
    public Int32? CustomerLocationRowUID { get; set; }

    public AssetLocation CustomerLocation { get; set; }

    [Column(TypeName = "nvarchar(50)")]
    public string WorkOrderType { get; set; }

    [Column(TypeName = "nvarchar(25)")]
    public string IssueType { get; set; }

    [Column(TypeName = "nvarchar(40)")]
    public string AssetItemCode { get; set; }

    [Column(TypeName = "nvarchar(40)")]
    public string AssetSerialNo { get; set; }

    [ForeignKey("AssetRegister")]
    public int? AssetRegisterROWUID { get; set; }

    public AssetRegister AssetRegister { get; set; }

    [Column(TypeName = "nvarchar(40)")]
    public string ProjectDirectoryID { get; set; }

    [Column(TypeName = "nvarchar(10)")]
    public string Priority { get; set; }

    [Column(TypeName = "nvarchar(25)")]
    public string Status { get; set; }

    [Column(TypeName = "nvarchar(25)")]
    public string StatusForClient { get; set; }


    public bool? Billable { get; set; }

    [Column(TypeName = "decimal(18, 2)")]
    public decimal? QuotedFee { get; set; }


    [Column(TypeName = "datetime")]
    public DateTime? DueDate { get; set; }

    [Column(TypeName = "nvarchar(240)")]
    public string Description { get; set; }

    [Column(TypeName = "nvarchar(50)")]
    public string ReportedBy { get; set; }


    [Column(TypeName = "datetime")]
    public DateTime? ReportedDate { get; set; }

    [Column(TypeName = "nvarchar(35)")]
    public string BusinessDataType { get; set; }

    [Column(TypeName = "nvarchar(240)")]
    public string DocRemarks { get; set; }

    [Column(TypeName = "nvarchar(2000)")]
    public string ErrorText { get; set; }


    public Guid? RowGlobalUID { get; set; }


    public Int32? HeaderROWUID { get; set; }


    [Column(TypeName = "datetime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime? DateOfDocument { get; set; }

    //[Timestamp]
    //public byte RowVersion { get; set; }

    //public IList<ServiceWorkOrderAttachment> Attachments { get; set; }

    public IList<ServiceWorkOrderDetails> Details { get; set; }


}

1 个答案:

答案 0 :(得分:0)

确定要创建ITEM吗?并指出您尝试保存行的表?

如果您想在表中插入行,则必须先创建您的商品。 f.ex:

var = new ServiceWorkOrders(){Column1=var1,Column2=var2 etc.};
await _context.ServiceWorkOrders.AddAsync<ServiceWorkOrders>(item);
await _context.SaveChangesAsync();