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

How to make a Property nullable in web api


For a model file which looks like:

public class WebApiEntity
{
  public string id { get; set; }
  public Collection<CData> data { get; set; }
}


public class CData
{
  public string CType { get; set; }

  public string CId { get; set; }
}

When I generate an ODATA client using service reference, it looks something like:

<EntityType Name="WebApiEntity">
    <Key>
    <PropertyRef Name="id" />
    </Key>
    <Property Name="id" Type="Edm.String" Nullable="false" />
    <Property Name="data" Type="Collection(NamespaceValue.CData)" Nullable="false" />
</EntityType>

It has nullable property set to FALSE. What do I have to do to set it to TRUE. It seems Nullable<> can't be used here. Appreciate any help here.


Solution

  • Please try this:

    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    var collectionProperty = builder.EntityType<WebApiEntity>().CollectionProperty<CData>(c=>c.data);
    collectionProperty.IsOptional();