title: Entity Framework crashes when used to back an MVVM application
body: <p>I am trying to use Entity Framework to provide the data model for a pure MVVM architecture.  It crashes with an 'Object reference not set to an instance of an object.' error after setting up a simple relationship that drives the view model from changes to the data model.</p>
  
  <p>The model is very simple: just a 'Products' table.  The View Model is a simple hierarchy with a 'Root' collection holding a list of <em>ProductCollection</em> items that are the view model analog of the 'Product' items in the data model.  Being an MVVM application, any changes to the data model are driven into the view model through event handlers.  The Entity Framework event handlers appear to be incapable of even modest cascading of events.  Hooking up the event handlers appears to crash the EF.</p>
  
  <p>The first event handler is installed here:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="RootCollection"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="compositionContainer"&gt;The composition container.&lt;/param&gt;
      public RootCollection(LicenseEntities licenseEntities)
      {
          // Initialize the object.
          this.nameField = "Root";
          this.licenseEntities = licenseEntities;
  
          // Populate the root with the products.
          foreach (Product product in this.licenseEntities.Products.ToList&lt;Product&gt;())
          {
              ProductCollection productCollection = new ProductCollection(
                  this.licenseEntities);
              productCollection.ProductId = product.ProductId;
              productCollection.Name = product.Name;
              this.Add(productCollection);
          }
  
          // This will keep the view model reconcilled to the data model when the
          // products change.
          this.licenseEntities.Products.Local.CollectionChanged +=
              this.OnProductChanged;
      }
  </code></pre>
  
  <p>And the second one is installed when each of the Product view models is instantiated:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="ProductCollection"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="licenseEntities"&gt;The DBContext for the license entities.&lt;/param&gt;
      public ProductCollection(LicenseEntities licenseEntities)
      {
          // Initialize the object.
          this.licenseEntities = licenseEntities;
  
          // Hook this object into the local collection change events.  This line
          // apparently kills Entity Framework.  Comment it out to allow the products
          // to be added to the model without throwing the exception.
          this.licenseEntities.Products.Local.CollectionChanged +=
             this.OnProductsChanged;
      }
  </code></pre>
  
  <p>The exception occurs when instantiating the product:</p>
  
  <pre><code>                licenseEntities.Products.Add(
                      new Product
                      {
                          Name = "Explorer Chrome Suite",
                          ProductId = MainWindowViewModel.explorerChromeSuite
                      });
  </code></pre>
  
  <p>If you remove the second event handler in the code above, it works, but won't update the name of the product in the view model when the underlying data model changes. This is really basic MVVM stuff and I have trouble believing that Entity Framework is this fragile and/or unsuited to MVVM patterns.  The full solution to reproduct the problem can be downloaded from:</p>
  
  <p><a href="http://www.teraque.com/Download/EF%20Problem%20Demo.zip" rel="nofollow">http://www.teraque.com/Download/EF%20Problem%20Demo.zip</a></p>