2
votes

I have 2 entities (Orders and Products) I expose as OData with ODataModelBuilder. In Order entity, there's a Status complex type. Is there a way to expose Status complex type?

ODataModelBuilder _modelBuilder = new ODataModelBuilder();

var _status = _modelBuilder.ComplexType<Status>();
_status.Property(x => x.Description);
_status.Property(x => x.Name);
_status.Property(x => x.StatusId);

var _order = _orders.EntityType;
_order.HasKey(x => x.OrderId);
_order.Property(x => x.ProductId);
_order.Property(x => x.Quantity);
_order.ComplexProperty(x => x.Status);

var _product = _products.EntityType;
_product.HasKey(x => x.ProductId);
_product.Property(x => x.Name);
_product.Property(x => x.Description);

Another way I could think of is to convert Status to EntityType. However, with this approach I can't define Status ComplexProperty in Order entity type, thus, removing Status property from Order type. The Order entity type must have Status.

Has anybody come across this problem before with OData in Web API ?

1
Your model building code looks fine to me. Are you saying that order doesn't have a complex property status in the created model?RaghuRam Nadiminti
I think he needs a routing convention for properties.Youssef Moussaoui
It's fine and working perfectly. Order does have Status complex property. What I want to do is to expose Status as OData, so I can do this: localhost/OData/Status to see list of available status.stack247

1 Answers

3
votes

There doesn't seem to be a way to do exactly what you want to do. However, you can certainly work around the issue.

public class Status
{
   // whatever you have here...
}

// essentially create a duplicate class
public class DerivedStatus : Status { }

// using modelBuilder...
modelBuilder.ComplexType<Status>();
modelBuilder.EntitySet<DerivedStatus>("Statuses");

Less than ideal, but it seems to work. From what I can see, you will have to remove the call to ComplexProperty as well. Let me know if that works for you.