1

I am trying to use patch to update a record in OData but the table consists of multiple primary keys so it is giving error as "The number of primary key values passed must match number of primary key values defined on the entity. ASP.NET MVC Entity Framework"

Here is the sample code.

    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);
    }

I need to pass 4 primary key parameter here:

PreferenceFormat PreferenceFormat = await PreferenceFormats.FindAsync(_userId);

How can I achieve the same.

T.S.
  • 18,195
  • 11
  • 58
  • 78

1 Answers1

0

I may be late to the party but the FindAsync has following definition

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

Hence, you can use multiple keys.

From MSDN: The ordering of composite key values is as defined in the EDM, which is in turn as defined in the designer, by the Code First fluent API, or by the DataMember attribute.

T.S.
  • 18,195
  • 11
  • 58
  • 78