title: NullReferenceException on object initialized with FormatterServices.GetUninitializedObject
body: <p>I'm mocking out a DbContext, and in my tests I want to ensure entities are marked for being deletion. As I mark each object in my mocked context, I track each entity with a static Dictionary:</p>
  
  <pre><code>public DbEntityEntry&lt;T&gt; Entry&lt;T&gt;(T entity) where T : class
  {
      var entry = (DbEntityEntry&lt;T&gt;)FormatterServices.GetUninitializedObject(typeof(DbEntityEntry&lt;T&gt;));
  
      var state = entityStates[entity];
      entry.State = state;
  
      return entry;
  }
  
  public void SetEntityState(object entity, System.Data.EntityState state)
  {
      entityStates.Add(entity, state);
      Entry(entity).State = state;
  }
  
  private static Dictionary&lt;object, EntityState&gt; entityStates = new Dictionary&lt;object, EntityState&gt;();
  </code></pre>
  
  <p>I couldn't get the <code>DbEntityEntry</code> instantiated with <code>Activator.CreateInstance()</code>, so I've instantiated with <code>FormatterServices.GetUninitializedObject()</code>.</p>
  
  <p>The issue is this line:</p>
  
  <pre><code>entry.State = state;
  </code></pre>
  
  <p>Trying to assign to this property results in:</p>
  
  <pre><code> NullReferenceException: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>Obviously the object isn't initialized given the name of the function, but I'm unsure why I can't assign to the object's properties without throwing this exception.</p>
  
  <p>FWIW, the test looks something like:</p>
  
  <pre><code> TripManager.DeleteTripsByForeignKey(int carId);
  
   var trip1 = _fakeDbContext.Trips.FirstOrDefault(t =&gt; t.TripId == 1);
  
   var entry = _fakeDbContext.Entry(trip1);
  
   Assert.AreEqual(EntityState.Deleted, entry.State);
  </code></pre>