Search code examples
c#asp.netasp.net-web-apiodata

How do I patch/update foreign key with OData.Delta?


Trying to make use of System.Web.Http.OData.Delta to implement PATCH methods in ASP.NET Web API services, but it seems unable to apply changes to foreign key relation properties.

consider this data type, which it should be possible to update in a PATCH request to the Web API service:

public class Company
{
    public int ID { get; set; }
    // [Required]
    public String Name { get; set; }
    public string Description { get; set; }
    public int ? CountryID { get; set; }
    [ForeignKey("CountryID")]
    public virtual Country Country { get; set; }

}

Every thing works fine when I update name/ description properties.However, when i try to update the country Id property,I can see following request originating from browser.

CountryID: 3
Description: "e"
ID: 10
Name: "EF" 

But at server side both country object and country id property is always null in Delta<Company> object.

Any idea how to upate foreign keys using delta/patch?


Solution

  • Just found that OData Delta<t> only update properties that are basic types or classes. Inherited classes, nullables, generics, integer,derived types aren't supported yet. (May be supported in future releases).

    I have solved this problem using deserialization method from a sample available here:

    Link