Search code examples
c#odata

Bug with OData virtual navigation property


I have a parent and child class:

public class SharedCarBundle : SharedEntity, ISharedCarBundle, ISharedEntity
    {
        public SharedCarBundle();

        public virtual SharedCar Car { get; set; }

    }

    public class CarBundle : SharedCarBundle, ISharedCarBundle, ICarAssigned
    {

          [Value]
        public new virtual Car Car { get; set; }

        /// <summary>
        /// The Car the bundle is of
        /// </summary>
        ISharedCar ISharedCarBundle.Car
        {
            get
            {
                return Car;
            }
            set
            {
                Car = (Car)value;
            }
        }
    }

and while using Odata Get Request, I always get back only the null value of the parent attribute and not the original value which lies in Car prop in child class.

what am I doing wrong and what should I do to retrieve the correct value? Note: The virtual property cant be changed.


Solution

  • Finally, I have found a solution to that myself. the problem was I was inheriting from several classes and obviously, when a navigation property is defined virtual in a parent class, Odata will only see the null value of the parent class and would never recognize the overridden property in the child class.

    Thus my advice is when working with Odata, please use only flat class structure and if you need to work with inheritance, use only interfaces and the class that you will pass it to Odata model should not inherit from other classes but only from interfaces.