Database Reference
In-Depth Information
Solution
Suppose that we have a model for speakers and the talks prepared for various conferences. The model might look
something like the one in Figure 8-8 .
Figure 8-8. A model with a many-to-many association between speakers and the talks they prepare
The first thing to note in our model is that Speaker and Talk are in a many-to-many association. We have, through
an independent association (and in an intermediate SpeakerTalk table in the database), a model that supports many
speakers for any given talk and many talks for any given speaker.
We want to control manually the synchronization between our object graph and the Change Tracker. We will do
this by calling the DetectChanges() method. Along the way, we'll illustrate how the synchronization is progressing.
Follow the pattern in Listing 8-10 to synchronize manually your POCO object graph with the Change Tracker.
Listing 8-10. Using DetectChanges() Explicitly When Required to Synchronize the Change Tracker Manually
class Program
{
static void Main(string[] args)
{
RunExample();
}
static void RunExample()
{
using (var context = new EFRecipesEntities())
{
context.Configuration.AutoDetectChangesEnabled = false;
var speaker1 = new Speaker { Name = "Karen Stanfield" };
var talk1 = new Talk { Title = "Simulated Annealing in C#" };
speaker1.Talks = new List<Talk> { talk1 };
// associations not yet complete
Console.WriteLine("talk1.Speaker is null: {0}",
talk1.Speakers == null);
context.Speakers.Add(speaker1);
 
Search WWH ::




Custom Search