locked
WCF Custom Data Service - how to set Nullable property on variables? RRS feed

  • Question

  • I have created a Custom Data Service, and now I want full control over which variables that should be allowed to be set as null and not.

     

    If i navigate to the $metadata for my service, I find eg:


     

    <Property Name="Description" Type="Edm.String" Nullable="true" />

     




    My metadata implementation for this property looks like this:

     

    var description = new ResourceProperty(
    
            "Description",
    
            ResourcePropertyKind.Primitive,
    
            ResourceType.GetPrimitiveResourceType(typeof(string))
    
          );

     

    How do I control the Nullable field for a specific variable?

     

    Monday, August 16, 2010 11:37 AM

Answers

  • Hi,

    For primitive types, the WCF Data Service support nullable/nonnullable in the same way CLR supports it. So for example System.In32 will be non-nullable. Nullable<System.Int32> will be nullable. String is always nullable in CLR, and thus it is also always nullable in WCF Data Services.

    The only exception is with key properties. If the property is a key property it is always non-nullable (regardless of the CLR type). WCF Data Services serialization will fail if a key property has null value.

    So to answer your question above, you can't change the Nullable field for string properties, it is always true for now.

    That is for the $metadata. Your IUpdatable can always validate the value and fail if the client tries to set it to null.

    Note that the our client side code gen (VS) will also ignore the nullable field on string typed properties, again because CLR has no way to declare a non-nullable string.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by Martinfy Monday, August 16, 2010 2:04 PM
    Monday, August 16, 2010 1:26 PM
    Moderator

All replies

  • Hi,

    For primitive types, the WCF Data Service support nullable/nonnullable in the same way CLR supports it. So for example System.In32 will be non-nullable. Nullable<System.Int32> will be nullable. String is always nullable in CLR, and thus it is also always nullable in WCF Data Services.

    The only exception is with key properties. If the property is a key property it is always non-nullable (regardless of the CLR type). WCF Data Services serialization will fail if a key property has null value.

    So to answer your question above, you can't change the Nullable field for string properties, it is always true for now.

    That is for the $metadata. Your IUpdatable can always validate the value and fail if the client tries to set it to null.

    Note that the our client side code gen (VS) will also ignore the nullable field on string typed properties, again because CLR has no way to declare a non-nullable string.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by Martinfy Monday, August 16, 2010 2:04 PM
    Monday, August 16, 2010 1:26 PM
    Moderator
  • Thanks! That explains why the ResourceProperty doesn't have any methods for specifying so.

    Monday, August 16, 2010 2:04 PM