OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
857 stars 473 forks source link

WEB API Unit of Work, Batch SetLink Issue #515

Open JoaoGlose opened 9 years ago

JoaoGlose commented 9 years ago

Hi all,

I have a Web Service Web API with Odata requests.

I have implemented Unit of Work and Batch handler.

I've been struggling with a batch request that have 1:1 related entities.

This entities are new and don't exist in the DB.

Example:

var work= new work();

var memo= new memo();

ctx.AddTowork(work); ctx.AddTomemo(memo); ctx.SetLink(work, nameof(memo), work.memo); ctx.SaveChanges(SaveChangesOptions.Batch);

The Odata request is bellow:

--batch_a1415f8f-7f90-4867-b301-14bba877c3b6 Content-Type: multipart/mixed; boundary=changeset_a25e5f57-d69e-441f-b73f-f93248fd8740

--changeset_a25e5f57-d69e-441f-b73f-f93248fd8740 Content-Type: application/http Content-Transfer-Encoding: binary

POST http://localhost.fiddler:21387/odata/work HTTP/1.1 Content-ID: 1 DataServiceVersion: 3.0;NetFx MaxDataServiceVersion: 3.0;NetFx Content-Type: application/json;odata=minimalmetadata Accept: application/json;odata=minimalmetadata Accept-Charset: UTF-8 User-Agent: Microsoft ADO.NET Data Services Authorization: Tc1U0FWiG76ygeaQxyO2dq6/Tv8QMPAzkliYSZJ3WyKUpfWYuzKO4bSUTBVgMu9L8vHDX7yIwxg=

{"odata.type":"work"," ........................... --changeset_a25e5f57-d69e-441f-b73f-f93248fd8740 Content-Type: application/http Content-Transfer-Encoding: binary

POST http://localhost.fiddler:21387/odata/memo HTTP/1.1 Content-ID: 2 DataServiceVersion: 3.0;NetFx MaxDataServiceVersion: 3.0;NetFx Content-Type: application/json;odata=minimalmetadata Accept: application/json;odata=minimalmetadata Accept-Charset: UTF-8 User-Agent: Microsoft ADO.NET Data Services Authorization: Tc1U0FWiG76ygeaQxyO2dq6/Tv8QMPAzkliYSZJ3WyKUpfWYuzKO4bSUTBVgMu9L8vHDX7yIwxg=

{"odata.type":"memo","stamp ....... --changeset_a25e5f57-d69e-441f-b73f-f93248fd8740 Content-Type: application/http Content-Transfer-Encoding: binary

PUT $1/$links/memo HTTP/1.1 Content-ID: 3 DataServiceVersion: 3.0;NetFx MaxDataServiceVersion: 3.0;NetFx Accept: application/json;odata=minimalmetadata Accept-Charset: UTF-8 Content-Type: application/json;odata=minimalmetadata User-Agent: Microsoft ADO.NET Data Services Authorization: Tc1U0FWiG76ygeaQxyO2dq6/Tv8QMPAzkliYSZJ3WyKUpfWYuzKO4bSUTBVgMu9L8vHDX7yIwxg=

{"url":"$2"} --changeset_a25e5f57-d69e-441f-b73f-f93248fd8740-- --batch_a1415f8f-7f90-4867-b301-14bba877c3b6--

My problem is when I'm inserting the "memo" entity it throws a foreign key exception.

I have the "CreateLink" Method in webAPI but before it reaches "PUT $1/$links/memo" it throws an Exception in the second request

"POST http://localhost.fiddler:21387/odata/memo HTTP/1.1" .

In each Controller i have to savechanges:

this.GetWorkBll.Update(work); this.GetUnitOfWork.SaveChanges(); If i ignore the savechanges for the batch request and only do it in the end it works well. (Because work entity is still in the context?)

What is the best way of doing a batch request with two new related entities (1 to 1 relation)?

I don't know if checking if it is a batch request is the best way to solve this.

If it is, how do I check in the controller that this is a batch request?

Kind regards,

João

congysu commented 9 years ago

@JoaoGlose Would you like to give it a try with RESTier: https://nuget.org/Packages/Microsoft.Restier . It supports batch with transaction.

RemcoBlok commented 9 years ago

RESTier currently has an issue to do with Content-ID references in batches as well, see https://github.com/OData/RESTier/issues/82 This is also not the only issue in WebApi related to Content-ID references in batches, see https://github.com/OData/WebApi/issues/512 Any update on when we may expect these issues to be resolved? many thanks Remco