stack247
stack247

Reputation: 5747

Web API OData - Expose ComplexType with ODataModelBuilder

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 ?

Upvotes: 2

Views: 4312

Answers (1)

Mark D
Mark D

Reputation: 3327

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.

Upvotes: 3

Related Questions