none
The entity type Document is not part of the model for the current context RRS feed

  • Question

  • I am using EF6.1 with unitofwork. When I try to save into the database I get "The entity type Document is not part of the model for the current context ". + Local '_dbSet.Local' threw an exception of type 'System.InvalidOperationException' System.Collections.ObjectModel.ObservableCollection<Model.Models.Document> {System.InvalidOperationException}. Below is the code 

     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>();
                    }
                }
            }
    
            public virtual TEntity Find(params object[] keyValues)
            {
                return _dbSet.Find(keyValues);
            }
    
            public virtual IQueryable<TEntity> SelectQuery(string query, params object[] parameters)
            {
                return _dbSet.SqlQuery(query, parameters).AsQueryable();
            }
    
            public virtual void Insert(TEntity entity)
            {
                entity.ObjectState = ObjectState.Added;
                _dbSet.Attach(entity);
                _context.SyncObjectState(entity);
            }
    
            public virtual void InsertRange(IEnumerable<TEntity> entities)
            {
                foreach (var entity in entities)
                {
                    Insert(entity);
                }
            }
    
     
            public virtual void InsertGraphRange(IEnumerable<TEntity> entities)
            {
                _dbSet.AddRange(entities);
            }
    
            public virtual void Update(TEntity entity)
            {
                entity.ObjectState = ObjectState.Modified;
                _dbSet.Attach(entity);
                _context.SyncObjectState(entity);
            }
    
            public virtual void Delete(object id)
            {
                var entity = _dbSet.Find(id);
                Delete(entity);
            }
    
            public virtual void Delete(TEntity entity)
            {
                entity.ObjectState = ObjectState.Deleted;
                _dbSet.Attach(entity);
                _context.SyncObjectState(entity);
            }
    
            public IQueryFluent<TEntity> Query()
            {
                return new QueryFluent<TEntity>(this);
            }
    
            public virtual IQueryFluent<TEntity> Query(IQueryObject<TEntity> queryObject)
            {
                return new QueryFluent<TEntity>(this, queryObject);
            }
    
            public virtual IQueryFluent<TEntity> Query(Expression<Func<TEntity, bool>> query)
            {
                return new QueryFluent<TEntity>(this, query);
            }
    
            public IQueryable<TEntity> Queryable()
            {
                return _dbSet;
            }
    
            public IRepository<T> GetRepository<T>() where T : class, IObjectState
            {
                return _unitOfWork.Repository<T>();
            }
    
            public virtual async Task<TEntity> FindAsync(params object[] keyValues)
            {
                return await _dbSet.FindAsync(keyValues);
            }
    
            public virtual async Task<TEntity> FindAsync(CancellationToken cancellationToken, params object[] keyValues)
            {
                return await _dbSet.FindAsync(cancellationToken, keyValues);
            }
    
            public virtual async Task<bool> DeleteAsync(params object[] keyValues)
            {
                return await DeleteAsync(CancellationToken.None, keyValues);
            }
    
            public virtual async Task<bool> DeleteAsync(CancellationToken cancellationToken, params object[] keyValues)
            {
                var entity = await FindAsync(cancellationToken, keyValues);
    
                if (entity == null)
                {
                    return false;
                }
    
                entity.ObjectState = ObjectState.Deleted;
                _dbSet.Attach(entity);
    
                return true;
            }
    
             
            internal IQueryable<TEntity> Select(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                List<Expression<Func<TEntity, object>>> includes = null,
                int? page = null,
                int? pageSize = null)
            {
                IQueryable<TEntity> query = _dbSet;
    
                if (includes != null)
                {
                    query = includes.Aggregate(query, (current, include) => current.Include(include));
                }
                if (orderBy != null)
                {
                    query = orderBy(query);
                }
                if (filter != null)
                {
                    query = query.AsExpandable().Where(filter);
                }
                if (page != null && pageSize != null)
                {
                    query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
                }
                return query;
            }
    
            internal async Task<IEnumerable<TEntity>> SelectAsync(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                List<Expression<Func<TEntity, object>>> includes = null,
                int? page = null,
                int? pageSize = null)
            {
                return await Select(filter, orderBy, includes, page, pageSize).ToListAsync();
            }
    
            public virtual void InsertOrUpdateGraph(TEntity entity)
            {
                SyncObjectGraph(entity);
                _entitesChecked = null;
                _dbSet.Attach(entity);
            }
    
            HashSet<object> _entitesChecked; // tracking of all process entities in the object graph when calling SyncObjectGraph
    
            private void SyncObjectGraph(object entity) // scan object graph for all 
            {
                if (_entitesChecked == null)
                    _entitesChecked = new HashSet<object>();
    
                if (_entitesChecked.Contains(entity))
                    return;
    
                _entitesChecked.Add(entity);
    
                var objectState = entity as IObjectState;
    
                if (objectState != null && objectState.ObjectState == ObjectState.Added)
                    _context.SyncObjectState((IObjectState)entity);
    
                // Set tracking state for child collections
                foreach (var prop in entity.GetType().GetProperties())
                {
                    // Apply changes to 1-1 and M-1 properties
                    var trackableRef = prop.GetValue(entity, null) as IObjectState;
                    if (trackableRef != null)
                    {
                        if (trackableRef.ObjectState == ObjectState.Added)
                            _context.SyncObjectState((IObjectState)entity);
    
                        SyncObjectGraph(prop.GetValue(entity, null));
                    }
    
                    // Apply changes to 1-M properties
                    var items = prop.GetValue(entity, null) as IEnumerable<IObjectState>;
                    if (items == null) continue;
    
                    Debug.WriteLine("Checking collection: " + prop.Name);
    
                    foreach (var item in items)
                        SyncObjectGraph(item);
                }
            }
        }
    }


    Ebenezer

    Friday, October 30, 2015 6:50 PM

Answers

  • >>When I try to save into the database I get "The entity type Document is not part of the model for the current context ".

    Will you use code first approach? If so, this issue is usually caused that the “Document” table is not created on startup, please have a check your database, and if it is, please add this code to OnModelCreating method:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity< Document >().ToTable("Document ");
    }

    If you are not in this scenario, please tell what mode you are using and also share the database/model schema with us.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by denkyira Monday, November 2, 2015 1:26 PM
    Monday, November 2, 2015 2:58 AM
    Moderator

All replies

  • >>When I try to save into the database I get "The entity type Document is not part of the model for the current context ".

    Will you use code first approach? If so, this issue is usually caused that the “Document” table is not created on startup, please have a check your database, and if it is, please add this code to OnModelCreating method:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity< Document >().ToTable("Document ");
    }

    If you are not in this scenario, please tell what mode you are using and also share the database/model schema with us.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by denkyira Monday, November 2, 2015 1:26 PM
    Monday, November 2, 2015 2:58 AM
    Moderator
  • Hi 

    Thanks for getting back to me. I have finally fixed it myself. I didn't know that the context querying the entity is not the same one inserting it. 


    Ebenezer

    Monday, November 2, 2015 1:26 PM