当我在 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;
}
应用补丁更改时运行的查询是
设置无计数;更新 [Records_Coworkers] SET [StartDate] = @p0 其中 [RecordID] = @p1 AND [CoworkerID] = @p2 AND [StartDate] 为 NULL;选择@@行计数;
这是模型
[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);
});