how to implement computed field for model in web api odata using EF 6

api entity-framework-6 odata web

Question

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.

1
1
7/11/2014 5:44:11 PM

Accepted Answer

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);
    }
3
7/12/2014 8:19:45 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow