【问题标题】:How to Patch using Delta with multiple primary keys in OData如何在 OData 中使用具有多个主键的 Delta 进行修补
【发布时间】:2015-10-15 06:11:03
【问题描述】:

我正在尝试使用补丁更新 OData 中的记录,但该表由多个主键组成,因此它给出了错误 “传递的主键值的数量必须与实体上定义的主键值的数量相匹配。ASP.NET MVC 实体框架”

这里是示例代码。

    public async Task<IHttpActionResult> Patch(Delta<PreferenceFormat> patch)
    {

        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        PreferenceFormat PreferenceFormat = await db.PreferenceFormats.FindAsync(_userId);

        if (PreferenceFormat == null)
        {
            return NotFound();
        }

        patch.Patch(PreferenceFormat);

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!PreferenceFormatExists(_userId))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(PreferenceFormat);
    }

这里需要传递4个主键参数:

PreferenceFormat PreferenceFormat = await PreferenceFormats.FindAsync(_userId);

我怎样才能达到同样的效果。

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api odata patch delta


    【解决方案1】:

    我可能迟到了,但FindAsync 有以下定义

    public virtual Task<object> FindAsync(params object[] keyValues)
    

    因此,您可以使用多个键。

    来自 MSDN:复合键值的顺序在 EDM 中定义,而 EDM 又在设计器中定义,由 Code First fluent API 或 DataMember 属性定义。

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 2021-02-02
      相关资源
      最近更新 更多