JsonPatchDocument applyTo()未使用所有属性,为什么?

时间:2019-05-03 19:15:59

标签: c# entity-framework entity-framework-core axios json-patch

当我在JsonPatchDocument上调用applyTo()然后保存更改时,EF运行的查询仅使用recordId,coworkerId和startDate进行更新。使用IsActive字段/列!为什么会这样,如果没有这种情况,我会得到DbUpdateConcurrencyException,因为现在行数多于1。

    public static async Task<HttpResult> PatchRecordCoworkerAsync(MpidDbContext context, int id, JsonPatchDocument<RecordCoworker> patch)
    {
        // fetches a single row
        var recordCoworker = context.RecordsCoworkers.SingleOrDefault(m => m.Id == id && m.IsActive == true);

        patch.ApplyTo(recordCoworker);

        try
        {
            await context.SaveChangesAsync();
        }
        // exception says "Database operation expected to affect 1 row(s) but actually affected 2 row(s)"
        catch (DbUpdateConcurrencyException ex)
        {
        }

        return HttpResult.NoContent;
    }

应用补丁更改时运行的查询是

  

将NOCOUNT设为ON;   UPDATE [Records_Coworkers] SET [StartDate] = @ p0   [RecordID] = @ p1 AND [CoworkerID] = @ p2 AND [StartDate] IS NULL;   选择@@ ROWCOUNT;

这是模型

[Table("Records_Coworkers")]
public class RecordCoworker
{
    public RecordCoworker()
    {
        IsActive = true;
        CreationDate = DateTime.Now;
    }

    [Key]
    [Column("Record_CoworkerID")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }

    [Required]
    public bool IsActive { get; set; }
    [Required]
    public DateTime CreationDate { get; set; }

    [Required]
    [Column("RecordID")]
    public int RecordId { get; set; }
    public virtual Record Record { get; set; }

    [Required]
    [Column("CoworkerID")]
    public int CoworkerId { get; set; }
    public virtual Coworker Coworker { get; set; }
}

是否有一种方法可以进行一些不同的配置,以便在补丁中使用'IsActive'属性?

我的axios呼叫看起来像这样

axios.patch('/api/RecordCoworkers/' + id, [{
  'op': 'replace',
  'path': '/startDate',
  'value': value,
}]).then(function(response) {
  // Show response
  console.log(response);
}).catch(function(error) {
  // Show error
  console.log(error);
});

0 个答案:

没有答案