Database Reference
In-Depth Information
7.
Next, from Listing 9-22, add the Recipe4ConnectionString connection string to the
Web.Config file under the ConnectionStrings section.
Listing 9-22. Connection string for the Recipe1 Web API Service
<connectionStrings>
<add name="Recipe4ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Then add the code in Listing 9-23 to the Application_Start method in the Global.asax
file. This code will disable the Entity Framework Model Compatibility check and instruct
the JSON serializer to ignore the self-referencing loop caused by navigation properties
being bidirectional between Customer and PhoneNumber.
8.
Listing 9-23. Disable the Entity Framework Model Compatibility Check
protected void Application_Start()
{
// Disable Entity Framework Model Compatibilty
Database.SetInitializer<Recipe1Context>(null);
// The bidirectional navigation properties between related entities
// create a self-referencing loop that breaks Web API's effort to
// serialize the objects as JSON. By default, Json.NET is configured
// to error when a reference loop is detected. To resolve problem,
// simply configure JSON serializer to ignore self-referencing loops.
GlobalConfiguration.Configuration.Formatters.JsonFormatter
.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore;
...
}
Next add a class entitled EntityStateFactory , and add the code from Listing 9-24 to it.
The factory will translate the TrackingState enum values exposed to the client to Entity
Framework state value required by the change tracking components.
9.
Listing 9-24. Customer Web API Controller
public static EntityState Set(TrackingState trackingState)
{
switch (trackingState)
{
case TrackingState.Add:
return EntityState.Added;
case TrackingState.Update:
return EntityState.Modified;
case TrackingState.Remove:
return EntityState.Deleted;
 
Search WWH ::




Custom Search