title: DbSet Object reference not set to an instance of an object
body: <p>I'm trying to make a universal 3-tier application with generic repositories for any data provider/orm. I just rewrote the code and got stuck on the error of Object reference not set to an instance of an object while trying make action in DbSet. I'm completely confused.. at least nudge me in the right direction.</p>
  
  <p><strong>Repository.cs</strong></p>
  
  <pre><code> public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
  
      public IUnitOfWork UnitOfWork { get; set; }
      private IDbSet&lt;T&gt; _objectset;
  
      private IDbSet&lt;T&gt; DbSet
      {
          get { return _objectset ?? (_objectset = UnitOfWork.Context.Set&lt;T&gt;()); }
      }
  public List&lt;T&gt; GetAll()
      {
          try
          {
              return DbSet.ToList();
          }
          catch (Exception)
          {
              throw new Exception("Error in repository while attempts get all elements");
          }
      }
  }
  </code></pre>
  
  <p><strong>EFUnitOfWork.cs</strong></p>
  
  <pre><code> public class EfUnitOfWork : IUnitOfWork
  {
      public EfUnitOfWork()
      {
          Context = new AppContext();
      }
  
      public DbContext Context { get; set; }
  
      public void Commit()
      {
          Context.SaveChanges();
      }
  
      public bool LazyLoadingEnabled
      {
          get { return Context.Configuration.LazyLoadingEnabled; }
          set { Context.Configuration.LazyLoadingEnabled = value; }
      }
  
      public void Dispose()
      {
          Context.Dispose();
      }
  }
  </code></pre>
  
  <p><strong>AppContext.cs</strong></p>
  
  <pre><code> public class AppContext : DbContext, IDbContext
  {
      public AppContext()
      {
          Database.SetInitializer(new MigrateDatabaseToLatestVersion&lt;AppContext,            Configuration&gt;());
          Configuration.ProxyCreationEnabled = false;
      }
  
      public DbSet&lt;Logic&gt; Logics { get; set; }
  
      public DbSet&lt;Category&gt; Categories { get; set; }
  
      public new IDbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class
      {
          return base.Set&lt;TEntity&gt;();
      }
  }
  </code></pre>
  
  <p><strong>IDbContext.cs</strong></p>
  
  <pre><code>public interface IDbContext
  {
      IDbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class;
      int SaveChanges();
      void Dispose();
  }
  </code></pre>
  
  <p><strong>Configuration.cs</strong></p>
  
  <pre><code>class Configuration : DbMigrationsConfiguration&lt;AppContext&gt;
  {
      public Configuration()
      {
          AutomaticMigrationsEnabled = true;
          AutomaticMigrationDataLossAllowed = true;
      }
  
      protected override void Seed(AppContext context)
      {
  
      }
  }
  </code></pre>