I'm creating a web API odata controller and all is well until I try to add a derived field to the model. I don't want the field to exist in the database, and the field does not need to be queryable.
Given model;
public class Foo
{
public Int64 Id { get; set; }
public string SomeDatabaseSourcedField{ get; set; }
public string SomeDerivedField{ get; set; }
}
If I've got a controller like;
private FooDbContext db = new FooDbContext();
[Queryable]
public IQueryable<Foo> GetFoo()
{
return db.Foo;
}
How do it set the value of SomeDerivedField
?
Note: I've tried marking the field as not mapped
but it seems the field does not get returned in API calls.
Here is my answer, What I'm using is odata v4. http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ I've successfully get the derived field in the response.
First, define a class which derives from foo:
public class MyFoo:Foo
{
public string SomeDerivedField{ get; set; }
}
Second, build the edm model:
public static IEdmModel GetModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<MyFoo>("Foo");
builder.EntityType<MyFoo>().DerivesFromNothing();
builder.Namespace = typeof(MyFoo).Namespace;
return builder.GetEdmModel();
}
Third, retrieve the data:
public IHttpActionResult Get()
{
IQueryable<MyFoo> foos= db.Foos.Select(
t => new MyFoo()
{
Id = t.Id,
Name = t.Name,
SomeDerivedField= t.Name + SqlFunctions.StringConvert((double)t.Id)
});
return Ok(foos);
}