Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Edited Unassigned: QueryableAttribute does not work when result model contains properties not populated in query [1435]

$
0
0
This initially looked like an Entity Framework problem, but the fact that the problem only occurs when OData parameters are included in the request makes me think it's probably an issue with the queries the OData components are constructing.

Consider this ApiController and the model it returns:
```
public class ItemController : ApiController
{
[Queryable]
public IQueryable<ItemModel> Get()
{
var context = new Context();

var itemModels = context.Items.Select(i =>
new ItemModel()
{
Name = i.Name,
Price = i.Price
});

return itemModels;
}
}


public class ItemModel
{
public string Name { get; set; }
public decimal Price { get; set; }

public string FormattedPrice
{
get { return Price.ToString("C0"); }
}
}
```

When a GET request is made to "/api/Error?$top=1", an exception is throw with the message "The specified type member 'FormattedPrice' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported." This is puzzling, because the FormattedPrice parameter is not used in the query.

If no OData parameters are passed, the request succeeds.

The request with OData parameters also succeeds if the code is changed as below:
```
public class ItemController : ApiController
{
[Queryable]
public IQueryable<ItemModel> Get()
{
var context = new Context();

var itemModels = context.Items.Select(i =>
new ItemModel()
{
Name = i.Name,
Price = i.Price,
FormattedPrice = "dummy value"
});

return itemModels;
}
}

public class ItemModel
{
public string Name { get; set; }
public decimal Price { get; set; }

public string FormattedPrice
{
get { return Price.ToString("C0"); }
set { return; /* dummy setter */ }
}
}
```

An example project which demonstrates the issue is attached.

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>