Unable to insert/Update database with UoW

Oct 31, 2015 at 2:12 AM
I have downloaded UoW and followed the source code to developed my project. I can query the database with the UoW, but whenever I try to do insert/Update I get this error below but the same code I can use my custom DbContext directly to insert/update.  
System.InvalidOperationException occurred
HResult=-2146233079
Message=The entity type Documents is not part of the model for the current context.
Source=EntityFramework
StackTrace:
   at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
   at System.Data.Entity.DbSet`1.Attach(TEntity entity)
   at MyProject.Test.DAL.Patterns.Repository`1.Insert(TEntity entity) in c:\Users\admin\Desktop\DocumentProject\MyProject.Test.DAL\Patterns\Repository.cs:line 64
InnerException:
  • Local '_dbSet.Local' threw an exception of type 'System.InvalidOperationException' System.Collections.ObjectModel.ObservableCollection<MyProject.Test.Documents> {System.InvalidOperationException}
Below is the code I am using
{
    #region Private Fields

    private readonly IDataContextAsync _context;
    private readonly DbSet<TEntity> _dbSet;
    private readonly IUnitOfWorkAsync _unitOfWork;

    #endregion Private Fields

    public Repository(IDataContextAsync context, IUnitOfWorkAsync unitOfWork)
    {
        _context = context;
        _unitOfWork = unitOfWork;

        // Temporarily for FakeDbContext, Unit Test and Fakes
        var dbContext = context as DbContext;

        if (dbContext != null)
        {
            _dbSet = dbContext.Set<TEntity>();
        }
        else
        {
            var fakeContext = context as FakeDbContext;

            if (fakeContext != null)
            {
                _dbSet = fakeContext.Set<TEntity>();
            }
        }
    }
Unit of work Constructor

#region Private Fields

    private IDataContextAsync _dataContext;
    private bool _disposed;
    private ObjectContext _objectContext;
    private DbTransaction _transaction;
    private Dictionary<string, dynamic> _repositories;

    #endregion Private Fields

    #region Constuctor/Dispose

    public UnitOfWork(IDataContextAsync dataContext)
    {
        _dataContext = dataContext;
        _repositories = new Dictionary<string, dynamic>();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        if (disposing)
        {
            // free other managed objects that implement
            // IDisposable only

            try
            {
                if (_objectContext != null && _objectContext.Connection.State == ConnectionState.Open)
                {
                    _objectContext.Connection.Close();
                }
            }
            catch (ObjectDisposedException)
            {
                // do nothing, the objectContext has already been disposed
            }

            if (_dataContext != null)
            {
                _dataContext.Dispose();
                _dataContext = null;
            }
        }

        // release any unmanaged objects
        // set the object references to null

        _disposed = true;
    }

    #endregion Constuctor/Dispose
Service Pattern

ublic abstract class Service<TEntity> : IService<TEntity> where TEntity : class, IObjectState
{
    #region Private Fields
    private readonly IRepositoryAsync<TEntity> _repository;
    #endregion Private Fields

    #region Constructor
    protected Service(IRepositoryAsync<TEntity> repository) { _repository = repository; }
    #endregion Constructor
//Implementations below//
The Db Context

    public partial class NorthwindContext : DataContext
{
    static NorthwindContext()
    {
        Database.SetInitializer<NorthwindContext>(null);
    }

    public NorthwindContext()
        : base("Name=NorthwindContext")
    {
    }        

    public DbSet<Category> Categories { get; set; }
    public DbSet<CustomerDemographic> CustomerDemographics { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Employee> Employees { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    //Other DBsets//
Nov 1, 2015 at 10:46 PM
I have finally figure out my issue. Leaving the Temporarily FakeDbContext was the issue.
        //// Temporarily for FakeDbContext, Unit Test and Fakes
        var dbContext = context as DbContext;

        if (dbContext != null)
        {
            _dbSet = dbContext.Set<TEntity>();
        }
        else
          var fakeContext = context as FakeDbContext;

            if (fakeContext != null)
           {
                _dbSet = fakeContext.Set<TEntity>();
            }
        }
Marked as answer by denkyira on 11/1/2015 at 3:47 PM