title: Moq testing null reference exception when testing entity framework repository Update()
body: <p>I'm trying to test my EF repository with MOQ.</p>
  
  <p>My context:</p>
  
  <pre><code>public class PhotoDbContext : DbContext, IDbContext
  {
      public PhotoDbContext(){} // to enable data migrations
  
      public PhotoDbContext(string connectionString, bool debugOutput = false) : base(connectionString)
      {
          if (debugOutput)
              // Write to output all sql operations
              Database.Log = s =&gt; Debug.WriteLine(s);
      }
  
      public virtual new IDbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class
      {
          return base.Set&lt;TEntity&gt;();
      }
  
      public virtual new void SaveChanges()
      {
          base.SaveChanges();
      }
  
      public new DbEntityEntry Entry&lt;TEntity&gt;(TEntity entity) where TEntity : class
      {
          return base.Entry(entity);
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          Configure(modelBuilder);
      }
  
      public new void Dispose()
      {
          base.Dispose();
      }
  
      public new DbContextConfiguration Configuration
      {
          get { return base.Configuration; }
      }
  </code></pre>
  
  <p>My repo:</p>
  
  <pre><code>public  class PhotoRepository : IPhotoRepository
  {
      private readonly IDbContext _context;
  
      public PhotoRepository(IDbContext context)
      {
          _context = context;
      }
  
      public void Save(Photo album)
      {
          _context.Set&lt;Photo&gt;().Add(album);
          _context.SaveChanges();
      }
  
      public void Update(Photo entity)
      {
          _context.Set&lt;Photo&gt;().Attach(entity);
          _context.Entry(entity).State = EntityState.Modified; **// null ref exception**
          _context.SaveChanges();
      }
  </code></pre>
  
  <p>And finally the test im trying to run.</p>
  
  <pre><code>    [TestMethod]
      public void UpdatePhoto()
      {
          var mockSet = new Mock&lt;DbSet&lt;Photo&gt;&gt;();
  
          var mockContext = new Mock&lt;PhotoDbContext&gt;();
          mockContext.Setup(m =&gt; m.Set&lt;Photo&gt;()).Returns(mockSet.Object);
  
          var service = new PhotoRepository(mockContext.Object);
  
          var photo = new Photo
          {
              Name = "123",
              Order = 2
          };
  
          service.Save(photo);
  
          mockSet.Verify(m =&gt; m.Add(It.IsAny&lt;Photo&gt;()), Times.Once);
          mockContext.Verify(m =&gt; m.SaveChanges(), Times.Once);
  
          photo.Name = "updated";
          service.Update(photo);
  
          mockSet.Verify(m =&gt; m.Add(It.IsAny&lt;Photo&gt;()), Times.Exactly(2));
          mockContext.Verify(m =&gt; m.SaveChanges(), Times.Exactly(2));
      }
  </code></pre>
  
  <p>The problem is I am getting a Null reference exception in repositories update method where I try to set <code>Entry.State = Entity.Modified</code></p>
  
  <p>I tried adding level of indirection in my context but it doesn't work.</p>
  
  <p>What am I missing? Any help would be appreciated.</p>