views:

41

answers:

1

I have a detached entity that has a navigation property as such:

public class ClassA
{
   public int Id { get; set; }
   public string Name { get; set; }

   public virtual ICollection<ClassB> ClassB
   {
      get
      {
         if (_classB == null)
         {
            var newCollection = new FixupCollection<ClassB>();
            newCollection.CollectionChanged += FixupClassB;
            _classB = newCollection;
         }
         return _classB;
      }
      set
      {
         if (!ReferenceEquals(_classB, value))
         {
            var previousValue = _classB as FixupCollection<ClassB>;
            if (previousValue != null)
            {
               previousValue.CollectionChanged -= FixupClassB;
            }
            _classB = value;
            var newValue = value as FixupCollection<ClassB>;
            if (newValue != null)
            {
               newValue.CollectionChanged += ClassB;
            }
         }
      }
   }
   private ICollection<ClassB> _classB;

   private void FixupClassB(object sender, NotifyCollectionChangedEventArgs e)
   {
      if (e.NewItems != null)
      {
         foreach (ClassB item in e.NewItems)
         {
            item.ClassA = this;
         }
      }

      if (e.OldItems != null)
      {
         foreach (ClassB item in e.OldItems)
         {
            if (ReferenceEquals(item.ClassA, this))
            {
               item.ClassA = null;
            }
         }
      }
   }
}

My create method works perfectly. It inserts the instance of ObjectA in the database and then inserts instances of ClassB from the collection into the database. The method simply does:

context.ClassA.AddObject(classA);

However, my update method does not insert/update/delete instances of ClassB when I call it. The method is as such:

public virtual void Update(List<ClassA> entities, TrialsContext context)
{
   foreach (ClassA entity in entities)
   {
      ClassA original = context.ObjectA.First(x => x.Id == entity.Id)

      if (original != null)
      {
         context.ObjectA.ApplyOriginalValues(original);
         context.ObjectA.ApplyCurrentValues(entity);
      }
   }
}

Is there any reason why this would fail? It works perfectly when I turn lazy loading off and fully hydrate my entities, however, this is a performance hit. What options do I have?

A: 

Must use Self Tracking Entities to accomplish goal.

Brandon