user1526912
user1526912

Reputation: 17220

How to work with OData primitives or complex types

I am working with OData for the first time and have a model entity with the following properties:

public IEnumerable<string> Genres { get; set; }
public IEnumerable<string> GenresFiltered { get; set; }

When I make the web call to retrieve data from the model I get the following message:

'The property 'Genres' on type 'xxxx' is not a valid property. Properties whose types are collection of primitives or complex types are not supported

Is there a way to workaround this error to display a list of strings in Odata?

Upvotes: 0

Views: 1223

Answers (1)

QianLi
QianLi

Reputation: 1098

Weird. I wrote a little test web api odata service and it works. The code are as below.

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Foo>("Foos");
        config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    }
}

The model class

public class Foo
{
    public string ID { get; set; }
    public IEnumerable<string> Genres { get; set; }
}

The controller class

public class FoosController : ODataController
{
    // GET odata/Foos
    [EnableQuery]
    public IHttpActionResult Get()
    {
        return Ok(FakeData.GetFoos().AsQueryable());
    }
}

And then I tried with http://localhost:37312/odata/Foos , the result is

{
"@odata.context": "http://localhost:37312/odata/$metadata#Foos",
"value": [
    {
        "ID": "1",
        "Genres": [
            "aaa",
            "bbb"
        ]
    },
    {
        "ID": "2",
        "Genres": [
            "ccc"
        ]
    }
]

}

Hope this can help.

Upvotes: 1

Related Questions