在基于EF模型的WCF数据服务项目中创建具有多个链接的条目
我正在尝试创建一个 WCF 数据服务,该服务公开由实体框架模型表示的数据库,并遇到针对具有多个 1-* 继承的表运行 HTTP POST 请求的问题。
实体框架表如下:
大多数具有一个父实体的表可以通过 POST 到父 URI 进行访问,即 'http://url/data.svc/parent(id)'。我遇到的问题是由于多重继承,我无法使用此方法。我可以允许一个 FK 可为空,并使用单独的 PUT 操作进行更新,但这只是糟糕的代码。
我正在使用 JSON 创建 HTTP 请求。这是我一直在尝试的示例
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)"}}
InitializeService 方法如下:
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
我知道这可能非常简单,但是有人可以帮助我弄清楚发布链接实体的语法吗?先感谢您
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, i.e., '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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够使用批量请求来完成此操作,该请求由一个发布新 OrderItem 的请求和一个更新指向其他实体的链接的请求组成。它应该类似于以下内容。
[Uri]/$batch
[1] POST [Uri]/Order(1)/Order_Item
[2] POST [Uri]/Item(1)/Order_Item/$link
看来您正在使用 Fiddler。您还可以使用 WCF 数据服务客户端并执行相同的工作,并了解请求在 Fiddler 中的外观。
有关如何使用 $batch 请求的信息:
http://msdn.microsoft.com/en-us/library/dd744839.aspx
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