Database Reference
In-Depth Information
else
Console.WriteLine("{0} ({1})", (int) _response.StatusCode, _response.ReasonPhrase);
}
}
12.
Finally, add the TravelAgent and Booking classes to this project just as we added to the
service in Listing 9-12.
Following is the output of the test client from Listing 9-18:
Successfully created Travel Agent John Tate and 2 Booking(s)
Successfully created Travel Agent Perry Como and 1 Booking(s)
Successfully updated Travel Agent Perry Como, Jr. and 2 Booking(s)
Travel Agent John Tate has 1 Booking(s)
Travel Agent Perry Como, Jr. has 2 Booking(s)
How It Works
Start by running the Web API application. The Web API application contains an MVC Web Controller, which, when
started, will bring up a home page. At this point, the site is running and its services are available.
Next open the console application, set a breakpoint on the first line of code in the
program.cs
file, and run the
console application. First we establish some basic plumbing—mapping the Web API service URI and configuring the
Accept Header
—that will ask the Web API service to return data in a JSON format.
We then call the
Cleanup
action method on the TravelAgent Web API controller using the
Client.DeleteAsync
method exposed by the
HttpClient
object. Cleanup truncates the database tables to clear data from any previous
operations.
Back in the client, we create a new travel agent and two bookings and then send these three new entities to the
service by calling the
PostAsync
from the
HttpClient
object. If you place a breakpoint in the
Update
Action Method
in the TravelAgent Web API controller class, you'll see that it receives the TravelAgent object as a parameter and adds
it to the TravelAgents entity in the context object. Doing so marks the object and all its related child entity objects as
added and causes the context to start tracking them.
■
it's worthwhile to mention that you should
Add
rather
Attach
a set of objects if there are multiple added
entities with the same value in the primary key property (in this case, multiple Bookings with an id = 0). if using
Attach
in this scenario, entity Framework will throw an exception because of the primary key conflicts (multiple entities with a
primary key = 0) in the non-added entities.
Note
We next check the Id property and make a somewhat crude determination that if it is greater than
0
, then this is
an existing entity, and we set the entity state property to
Modified
by calling the
Entry
method on the Context object.
Additionally, we set a flag entitled
newParentEntity
, so that we can return the correct Http Status Code later in the
operation. In the event that the Id property of TravelAgent is equal to
1
, then we leave the state property as
Added
.
We next iterate through each of the child booking objects applying the exact same logic. Upon completion, we
call the
SaveChanges
method, which generates SQL Update statements for
Modified
entities and Sql Insert statements
for
Added
entities. Then we return an Http Status Code of
201
for inserted entities and
200
for modified entities.
The 200 Http Status Code informs the calling program that the operation completed successfully; while
201
informs
the client that an insert operation completed successfully. When exposing REST-based services, it is a best practice to
return an Http Status Code to the calling program to verify the outcome of the operation.