C# - OData: Cannot build an System.Web.Http.OData.

2019-04-15 10:39发布

问题:

I am running a project in ASP.NET Web API 2 (not Core), where I have an object like this:

public class Desktop : EntityData
{
    public Desktop()
    {
    }

    public new Guid Id
    {
        get { return Guid.Parse(base.Id); }
        set { base.Id = value.ToString(); }
    }
    ...
}

public abstract class EntityData
{
    protected EntityData();
    public string Id { get; set; }
    public bool Deleted { get; set; }
}

As it can be seen, there is a property Id with the new operator.

This hinders me from creating a new Delta object, with any of the existing constructors:

Delta<Desktop> delta = new Delta<Desktop>();
Delta<Desktop> delta = new Delta<Desktop>(typeof(Desktop));
Delta<Desktop> delta = new Delta<Desktop>(typeof(Desktop), new string[] { "Id" });

In fact an exception is thrown by the constructor, saying

An item with the same key has already been added.

How can I successfully build the Delta Object?

Thank you very much!

cghersi