Search code examples
c#odata

OData - Adding fields to proxy partial class but need to ignore them in requests


I have an OData v4 service that I newly created. I have generated a proxy and connected to it successfully for all operations now.

I now want to add two properties in the generated proxy via a partial class sitting outside, example is given below:

public partial class ExchangeRate
{
    public DateTime? AddedOnDate { get; set; }
    public DateTime? UpdatedOnDate { get; set; }
}

Now when I go to query the service I get an error on the "client" side (my side of the code, not service) that there is no settable properties in DateTime. I get that DateTime is not supported and it should be DateTimeOffset, my point is to find a way in which the proxy will ignore these properties while connecting to the service, something like an "ignore" attribute or something.

I tried putting the "System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute" on each of the properties, it didn't work.

The error is originating from here : "Microsoft.OData.Client.ClientEdmModel.ValidateComplexTypeHasProperties"


Solution

  • Did it by making the properties internal, EF doesn't care wether the properties are public or not at least when I make a separate mapping class (EntityTypeConfiguration<>), OData proxy stopped picking those classes up.

    The properties look like this now:

    internal DateTime? AddedOnDateEF
        {
            get
            {
                if (!this.AddedOn.HasValue)
                    return null;
    
                return this.AddedOn.Value.DateTime;
            }
            set
            {
                if (!value.HasValue)
                    this.AddedOn = null;
                else
                {
                    this.AddedOn = new DateTimeOffset(value.Value);
                }
            }
        }
    

    and the entity configuration class has this line in its constructor:

    this.Property(t => t.AddedOnDateEF).HasColumnName("AddedOn");
    this.Ignore(t => t.AddedOn);
    

    The "AddedOn" property of type DateTimeOffset is in the OData proxy which is the other end of the partial class "ExchangeRate" shown in the example above.