简体   繁体   中英

Creating entries with multiple links in a WCF data services project based on EF model

I'm trying to create a WCF data service that exposes a database represented by an entity framework model, and running into a problem running HTTP POST requests against a table with multiple 1-* inheritances.

The entity framework table is as follows:模型

Most tables with one parent entity can be accessed via POST to the parent URI, ie, ' http://url/data.svc/parent(id) '. The problem I'm running into is I can't use this method due to the multiple inheritances. I could allow one FK to be nullable, and update with a separate PUT operation, but that's just bad code.

I'm creating the HTTP request using JSON. Here's an example of what I've been trying

POST http://url/data.svc/Order_Item HTTP/1.1
User-Agent: Fiddler
Accept: application/json
Content-Type: application/json

{"Count": 2, "Item": {"uri": "http://url/data.svc/Items(ID)"}, "Order": {"uri": "http://url/data.svc/Order(ID)"}}

The InitializeService method is as follows:

public static void InitializeService(DataServiceConfiguration config)
     {
          config.SetEntitySetAccessRule("*", EntitySetRights.All);
          config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
          config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
     }

I know that this is probably pretty simple, but could someone help me figure the syntax on posting linked entities? Thank you in advance

You should be able to do that using a batch request, composited of one request posting the new OrderItem, and one request to update the link towards other entity. It should be something similiar to the following.

[Uri]/$batch

[1] POST [Uri]/Order(1)/Order_Item

[2] POST [Uri]/Item(1)/Order_Item/$link

It seems that you are using Fiddler. You can also use WCF Data Service Client and do the same job, and get an idea on how the request looks like in Fiddler.

Information on how to use $batch request: http://msdn.microsoft.com/en-us/library/dd744839.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM