0
votes

i am building my the model using ODataModelBuilder, i am trying to create navigation property however in the metadata i dont see any foreginkey indication, in my solution i am not using EF, so there is no foreignKey attribute, is it possible to add it by code?

1
Just to clarify: Do you mean that in the $metadata document, you want to see a ReferentialConstraint element inside your Association? Or, are you just trying to figure out how to create a navigation between two entity sets using the ODataModelBuilder?Jen S
i was able to create navigation property, how ever when i test it with wcf data proxy - when i query for the main object the related entities are empty , although that i can se then in sniffer my guess it is related to foreign keyli-raz
By the wcf data proxy, I'm guessing you mean the DataServiceContext class (and related classes)? I think I understand your problem now, and I don't think it has to do with foreign keys (and I'll answer below). But I am confused that you say the related entity data shows up when you sniff the payload -- do you mean that you see links to the related entities? Or the actual data belonging to the related entities? I would expect that unless you are include a $expand query option in your request URI, the actual data of the related entities shouldn't be there.Jen S

1 Answers

2
votes

As you clarified in your comment, the reason you want to add foreign key information is because your client application is not including related entities when you query the main entity. I don't think foreign keys are the problem here.

As an example, I'll use two entity types: Customer and Order. Every Customer has some number of associated Orders, so I have a navigation property on Customer called Orders that points to a collection of Orders. If I issue a GET request to /MyService.svc/Customers(1), the server will respond with all of the Customer's information as well as URLs that point to the related Order entities*. I won't, by default, get the data of each related Order within the same payload.

If you want a request to Customers(1) to include all of the data of its associated Orders, you would add the $expand query option to the request URI: /MyService.svc/Customers(1)?$expand=Orders. Using the WCF Data Services client (DataServiceContext), you can do this with .Expand():

DataServiceQuery<Customer> query = context.Customers.Expand("Orders");

However, WebAPI OData doesn't currently support $expand (the latest nightly builds do though, so this will change soon).

The other approach would be to make a separate request to fill in the missing Order data. You can use the LoadProperty() method to do this:

context.LoadProperty(customer, "Orders");

The LoadProperty approach should work with WebAPI as it stands today.

I know this doesn't answer your original question, but I hope addresses your intent.

*In JSON, which is the default format for WebAPI OData services, no links will show up on the wire, but they are still there "in spirit". The client is expected to be able to compute them on its own, which the WCF Data Services Client does.