title: Entity Framework 4 SaveChanges not working and not throwing any error?
body: <p>I am trying to use my generic repository with a "unit of work" pattern.</p>
  
  <p>Here is my work details</p>
  
  <pre><code>public  class GenericRepository:IRepository
  {
  
      private readonly string _connectionStringName;
      private ObjectContext _objectContext;
      private readonly PluralizationService _pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en"));
  
      public GenericRepository()
      {
         this._objectContext = ContextManager.CurrentFor();
      }
  
      public void Add&lt;TEntity&gt;(TEntity entity) where TEntity : class
      {
        ((DataEntities.MyTestDBEntities)_objectContext).Countries.AddObject(new Country() { CountryName="UGANDA"});
        this._objectContext.AddObject(GetEntityName&lt;TEntity&gt;(), entity);
      }
  
      public void Update&lt;TEntity&gt;(TEntity entity) where TEntity : class
      {
          var fqen = GetEntityName&lt;TEntity&gt;();
  
          object originalItem;
          EntityKey key = ObjectContext.CreateEntityKey(fqen, entity);
          if (ObjectContext.TryGetObjectByKey(key, out originalItem))
          {
              ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
          }
      }
  
      private string GetEntityName&lt;TEntity&gt;() where TEntity : class
      {
          return string.Format("{0}.{1}", ObjectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
      }
  
      public object Get&lt;TEntity&gt;() where TEntity : class
      {
          var entityName = GetEntityName&lt;TEntity&gt;();
          return ObjectContext.CreateQuery&lt;TEntity&gt;(entityName);
      }
  
      public IEnumerable&lt;TEntity&gt; Find&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; criteria) where TEntity : class
      {
          return GetQuery&lt;TEntity&gt;().Where(criteria);
      }
  
      private IUnitOfWork unitOfWork;  
  
      public ObjectContext ObjectContext
      {
          get { return ContextManager.CurrentFor(); }
      }
  
       public IUnitOfWork UnitOfWork
      {
          get
          {
              if (unitOfWork == null)
              {
                  unitOfWork = new UnitOfWork(this.ObjectContext);
              }
              return unitOfWork;
          }
      }
  
      public IQueryable&lt;TEntity&gt; GetQuery&lt;TEntity&gt;() where TEntity : class
      {
           var entityName = GetEntityName&lt;TEntity&gt;();
           return ObjectContext.CreateQuery&lt;TEntity&gt;(entityName);
      }
  }
  </code></pre>
  
  <p>then I will redirect save changes and other committing the transaction with <code>UnitOfWork.cs</code></p>
  
  <pre><code>public class UnitOfWork:IUnitOfWork
  {
      private DbTransaction _transaction;
      private ObjectContext _objectContext;
  
      public UnitOfWork(ObjectContext context)
      {
          _objectContext = context;
      }
  
      public bool IsInTransaction
      {
          get { return _transaction != null; }
      }
  
      public void BeginTransaction()
      {
          BeginTransaction(IsolationLevel.ReadCommitted);
      }
  
      public void BeginTransaction(IsolationLevel isolationLevel)
      {
          if (_transaction != null)
          {
              throw new ApplicationException("Cannot begin a new transaction while an existing transaction is still running. " +
                                              "Please commit or rollback the existing transaction before starting a new one.");
          }
          OpenConnection();
          _transaction = _objectContext.Connection.BeginTransaction(isolationLevel);
      }
  
      public void RollBackTransaction()
      {
          if (_transaction == null)
          {
              throw new ApplicationException("Cannot roll back a transaction while there is no transaction running.");
          }
  
          try
          {
              _transaction.Rollback();
          }
          catch
          {
              throw;
          }
          finally
          {
              ReleaseCurrentTransaction();
          }
      }
  
      public void CommitTransaction()
      {
          if (_transaction == null)
          {
              throw new ApplicationException("Cannot roll back a transaction while there is no transaction running.");
          }
  
          try
          {
              _objectContext.SaveChanges();
              _transaction.Commit();
          }
          catch
          {
              _transaction.Rollback();
              throw;
          }
          finally
          {
              ReleaseCurrentTransaction();
          }
      }
  
      public void SaveChanges()
      {
          if (IsInTransaction)
          {
              throw new ApplicationException("A transaction is running. Call BeginTransaction instead.");
          }
          _objectContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
      }
  
      public void SaveChanges(SaveOptions saveOptions)
      {
          if (IsInTransaction)
          {
              throw new ApplicationException("A transaction is running. Call BeginTransaction instead.");
          }
          _objectContext.SaveChanges(saveOptions);
      }
  
      /// &lt;summary&gt;
      /// Releases the current transaction
      /// &lt;/summary&gt;
      private void ReleaseCurrentTransaction()
      {
          if (_transaction != null)
          {
              _transaction.Dispose();
              _transaction = null;
          }
      }
  
      private void OpenConnection()
      {
          if (_objectContext.Connection.State != ConnectionState.Open)
          {
              _objectContext.Connection.Open();
          }
      }
  
      /// &lt;summary&gt;
      /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
      /// &lt;/summary&gt;
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      /// &lt;summary&gt;
      /// Disposes the managed and unmanaged resources.
      /// &lt;/summary&gt;
      /// &lt;param name="disposing"&gt;&lt;/param&gt;
      private void Dispose(bool disposing)
      {
          if (!disposing)
              return;
  
          if (_disposed)
              return;
  
          ReleaseCurrentTransaction();
  
          _disposed = true;
      }
      private bool _disposed;
  }
  </code></pre>
  
  <p>and I am getting my context through my <code>ContextManager</code> class:</p>
  
  <pre><code>public class ContextManager
  {
      /// &lt;summary&gt;
      /// The default connection string name used if only one database is being communicated with.
      /// &lt;/summary&gt;
      public static readonly string DefaultConnectionStringName = "DefaultDb";
  
      /// &lt;summary&gt;
      /// An application-specific implementation of IObjectContextStorage must be setup either thru
      /// &lt;see cref="InitStorage" /&gt; or one of the &lt;see cref="Init" /&gt; overloads. 
      /// &lt;/summary&gt;
      private static IObjectContextStorage Storage { get; set; }
  
      /// &lt;summary&gt;
      /// Maintains a dictionary of object context builders, one per database.  The key is a 
      /// connection string name used to look up the associated database, and used to decorate respective
      /// repositories. If only one database is being used, this dictionary contains a single
      /// factory with a key of &lt;see cref="DefaultConnectionStringName" /&gt;.
      /// &lt;/summary&gt;
    //  private static Dictionary&lt;string, IObjectContextBuilder&lt;ObjectContext&gt;&gt; objectContextBuilders = new Dictionary&lt;string, IObjectContextBuilder&lt;ObjectContext&gt;&gt;();
  
      private static object _syncLock = new object();
  
      /// &lt;summary&gt;
      /// Used to get the current object context session if you're communicating with a single database.
      /// When communicating with multiple databases, invoke &lt;see cref="CurrentFor()" /&gt; instead.
      /// &lt;/summary&gt;
      public static ObjectContext Current
      {
          get { return CurrentFor(); }
      }
  
      /// &lt;summary&gt;
      /// Used to get the current ObjectContext associated with a key; i.e., the key 
      /// associated with an object context for a specific database.
      /// 
      /// If you're only communicating with one database, you should call &lt;see cref="Current" /&gt; instead,
      /// although you're certainly welcome to call this if you have the key available.
      /// &lt;/summary&gt;
      public static ObjectContext CurrentFor()
      {
          ObjectContext context = null;
          lock (_syncLock)
          {               
              if (context == null)
              {
                  context =new TestDAL.DataEntities.MyTestDBEntities();
                  //Storage.SetObjectContextForKey(key, context);
              }
          }
  
          return context;
      }
  
      /// &lt;summary&gt;
      /// This method is used by application-specific object context storage implementations
      /// and unit tests. Its job is to walk thru existing cached object context(s) and Close() each one.
      /// &lt;/summary&gt;
      public static void CloseAllObjectContexts()
      {
          if (CurrentFor().Connection.State == System.Data.ConnectionState.Open)
          {
              CurrentFor().Connection.Close();
          }
      }
  }
  </code></pre>
  
  <p>it gives me retrieval of entities, but when I want to create an entity it doesn't shows nay error nor any update in the database.</p>
  
  <p>Any clue will be helpful.</p>