Database Reference
In-Depth Information
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TravelAgent>().HasKey(x => x.AgentId);
modelBuilder.Entity<TravelAgent>().ToTable("Chapter9.TravelAgent");
modelBuilder.Entity<Booking>().ToTable("Chapter9.Booking");
}
}
6.
Next, from Listing 9-14, add the Recipe3ConnectionString connection string to the
Web.Config file under the ConnectionStrings section.
Listing 9-14. Connection String for the Recipe1 Web API Service
<connectionStrings>
<add name="Recipe3ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
7.
Then add the code in Listing 9-15 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 TravelAgent and Booking.
Listing 9-15. 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;
...
}
8.
Modify the Web API routing by changing the code in RouteConfig.cs file to match that
of Listing 9-16.
 
Search WWH ::




Custom Search