locked
How to expose derived Entities in the data service? RRS feed

  • Question

  • Re. ADO.Net Entity inheritance -

    The VS entity designer does not code generate EntitySets for derived classes; it only code gens sets for base classes.

    I posed this question in the Entity framework forum, and they showed a way to add an
    IQueryable property to the ObjectContext for a derived class.

    So, for example, with a DerivedClass that inherits from a BaseClass:

            public IQueryable<DerivedClass> DerivedClasses
            {
                get { return BaseClasses.OfType<DerivedClass>(); }
            }

    Now, I'm working with ADO.Net Data services (great stuff BTW), and I want to expose my derived classes in the web service - how can I do this?

    The data service that is generated exposes the BaseClasses set, but does not expose the DerivedClasses IQueryable member.

    Comments?   Thanks!
    Thursday, April 24, 2008 5:38 PM

Answers

  • Interesting problem - If your underlying provider is an edm provider, we only look at the metadata and figure out the entity sets. So if you added a top level IQueryable<> manually in the ObjectContext, today there is no way to expose this via the data service. I can see this as something pretty useful. I will open a DCR for this, but for the first version, the bar is pretty high and seems unlikely that we will be able to do something.

     

    Sorry, we didn't have a good answer for you, but thanks a lot for pointing out a good use case to us. We will definitely try and see if we can do something about this.

     

    Thanks

    Pratik

     

    Friday, April 25, 2008 5:06 AM
    Moderator

All replies

  • The URI syntax addresses Entity Sets and not Entity Types, so if a set is made up of a type hierarchy (ex. Customers Entity Set can have Customer or PreferredCustomer types) then you still access the set still via /Customers.  In the representation returned it will state what type each entity is (look for a "type" attribute in the response).  

     

    At present there does not exist a way to address properties of a derived type directly, but you can use the $filter operator to get at the derived type for some scenarios.  See the very bottom section of this blog post: http://blogs.msdn.com/marcelolr/archive/2008/01/15/arithmetic-and-built-in-functions-for-filter.aspx 

    Friday, April 25, 2008 12:48 AM
  • Interesting problem - If your underlying provider is an edm provider, we only look at the metadata and figure out the entity sets. So if you added a top level IQueryable<> manually in the ObjectContext, today there is no way to expose this via the data service. I can see this as something pretty useful. I will open a DCR for this, but for the first version, the bar is pretty high and seems unlikely that we will be able to do something.

     

    Sorry, we didn't have a good answer for you, but thanks a lot for pointing out a good use case to us. We will definitely try and see if we can do something about this.

     

    Thanks

    Pratik

     

    Friday, April 25, 2008 5:06 AM
    Moderator
  • Here's what you can do - expose a service operation which exposes this data. Here's how the code snippet with look like (this code lives in your service class)

     

    Code Snippet

    [WebGet]

    [SingleResult]

    public IQueryable<DerivedClass> DerivedClasses()

    {

        // Create a new instance of ObjectContext

        // return oc.DerivedClasses (the property you wrote on the ObjectContext class for exposing this data.

    }

     

     

    This way you can support GETs. There might be a way to do CUD also. Let me think some more about this.

     

    Thanks

    Pratik

    Friday, April 25, 2008 3:41 PM
    Moderator