title: Save Changes Does not work in Encapsulated UnitOFWork class
body: <p>Hi I am trying to create a generic repository using entity framework code first and encapsulate everything in a UnitOfWork but something must be wrong because when I trying to add that and use my encapsulated SaveChanges it does not work.
  Here is my repository code:</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      private DbContext Context { get; set; }
      private DbSet&lt;T&gt; DbSet
      {
          get { return Context.Set&lt;T&gt;(); }
      }
  
      public Repository(DbContext context)
      {
          Context = context;
      } 
  
      public virtual IEnumerable&lt;T&gt; GetAll()
      {
          return DbSet;
      }
  
      public virtual T GetById(int id)
      {
          return DbSet.Find(id);
      }
  
      public virtual void Add(T entity)
      {
          DbEntityEntry dbEntityEntry = Context.Entry(entity);
          if (dbEntityEntry.State != EntityState.Detached)
          {
              dbEntityEntry.State = EntityState.Added;
          }
          else
          {
  
              DbSet.Add(entity);
          }
      }
  
      public virtual void Update(T entity)
      {
          DbEntityEntry dbEntityEntry = Context.Entry(entity);
          if (dbEntityEntry.State == EntityState.Detached)
          {
              DbSet.Attach(entity);
          }
          DbSet.Attach(entity);
      }
  
      public virtual void Remove(T entity)
      {
          DbEntityEntry dbEntityEntry = Context.Entry(entity);
          if (dbEntityEntry.State != EntityState.Deleted)
          {
              dbEntityEntry.State = EntityState.Deleted;
          }
          else
          {
              DbSet.Attach(entity);
              DbSet.Remove(entity);   
          }
      }
  
      public virtual void Remove(int id)
      {
          var entity = GetById(id);
          if (entity == null)
          {
              return;
          }
          Remove(entity);
      }
  }
  </code></pre>
  
  <p>And here is my UnitOfWork code:</p>
  
  <pre><code>public class UnitOfWork
  {
      private readonly RepositoryFactory repositoryFactory;
  
      private DatabaseContext DbContext
      {
          get { return new DatabaseContext(); }
      }
  
      public IRepository&lt;Product&gt; Products
      {
          get
          {
              return repositoryFactory.GetRepository&lt;Product&gt;(DbContext);
          }
      }
  
      public UnitOfWork()
      {
          repositoryFactory = new RepositoryFactory();
      }
  
      public void SavaChanges()
      {
          DbContext.SaveChanges();
      }
  }
  </code></pre>
  
  <p>This is the code I am calling to Add data and get data:</p>
  
  <pre><code>var sa = new UnitOfWork();
  var repository = sa.Products;;
  var result = repository.GetAll();
  var resultbyId = repository.GetById(3);
  
   var product = new Product()
   {
       Name = "sddasd",
       CategoryId = 1,
       SubcategoryId = 1,
       Price = 21,
       Description = "dsadasfas",
       ImagePath = "Dsadas",
       NumberOfProducts = 29
    };
     repository.Add(product);
     sa.SavaChanges()
  </code></pre>
  
  <p>After running this code it seems that for some reason my SaveChanges incapsulated in my UnitOfWork class does not work.</p>
  
  <p>But if for example I would add this line right after DbSet.Add(entity)</p>
  
  <pre><code>Context.SaveChanges() 
  </code></pre>
  
  <p>It seems that the object get's added to the database.</p>
  
  <p>How can I make my UnitOfWork SaveChanges method to work?</p>