none
IGenericRepository<T> - Err The entity type Employee_Data is not part of the model for the current context ? RRS feed

  • Question

  • Hi

    I try to make and dll project for IGenericRepository to let me use it in any project

    and i set connection string name in my project web.config to be the same as I set it in the dll

     <connectionStrings>
        <add name="MyConnection" connectionString="data source=.;initial catalog=Dr_Emplo;persist security info=True;user id=ahmedamin;password=sasa2206;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    and when I try to get data using it from "Employee_Data" I type:

     public class HomeController : Controller
        {
            IUnitOfWork<Employee_Data> _uow;
    
            public HomeController()
            {
                _uow = new UnitOfWork<Employee_Data>();
            }
    
            public HomeController(IUnitOfWork<Employee_Data> uof) // Fake 
            {
                _uow = uof;
            }
    
            public ActionResult Index()
            {
                var x = _uow.TableName.FindByID(1);
                return View(x);
            }
        }

    but I got err:

    The entity type Employee_Data is not part of the model for the current context

    I don't know why I see that error and don't know what i must do to fix it - "did I must add something in OnModelCreating because I'm using code first" ?

    I do the following in my dll project:

    1. create DatabaseContext class:

     class DatabaseContext : DbContext
        {
            public DatabaseContext() : base("name=MyConnection")
            {
    
            }
        }

    2. Create IGenericRepository<T> interface:

     public interface IGenericRepository<T> where T : class
        {
            
            IQueryable<T> List(Expression<Func<T, bool>> filter = null);
    
            T FindByID(int id);
    
            void Add(T toAdd);
    
            void Edit(int id, T toUpdate);
    
            void Delete(int id);
        }

    3. Create EFGenericRepository<T> class:

    ublic class EFGenericRepository<T> : IGenericRepository<T> where T : class
        {
            DbContext db;
            IDbSet<T> entity;
    
            public EFGenericRepository(DbContext db)
            {
                this.db = db;
                this.entity = this.db.Set<T>();
            }
    
            public IQueryable<T> List(Expression<Func<T, bool>> filter = null)
            {
                if (filter != null)
                {
                    return entity.Where(filter); 
                }
                return entity;
            }
    
            // Find Data
            public T FindByID(int id)
            {
                return entity.Find(id);
            }
    
            // Insert Data
            public void Add(T entityToAdd)
            {
                entity.Add(entityToAdd);
            }
    
            // Update Data
            public void Edit(int id, T entitToUpdate)
            {
                entity.Attach(entitToUpdate);
                db.Entry<T>(entitToUpdate).State = EntityState.Modified;
            }
    
            // Delete Data
            public void Delete(int id)
            {
                T selectedEntity = entity.Find(id);
                db.Entry<T>(selectedEntity).State = EntityState.Deleted;
            }
        }

    4. Create IUnitOfWork<T> interface:

    public interface IUnitOfWork<T> : IDisposable where T : class
        {
            IGenericRepository<T> TableName { get; }
    
            void Save();
        }

    5. Create UnitOfWork<T> class:

    public class UnitOfWork<T> : IUnitOfWork<T>, IDisposable where T : class
        {
            DbContext _context;
    
            public UnitOfWork()
            {
                _context = new DatabaseContext();
            }
    
            private IGenericRepository<T> _EntityName;
    
            public IGenericRepository<T> TableName
            {
                get
                {
                    if (_EntityName == null)
                    {
                        return new EFGenericRepository<T>(_context);
                    }
                    return _EntityName;
                }
            }
    
            public void Dispose()
            {
                _context.Dispose();
                GC.SuppressFinalize(this);
            }
    
            public void Save()
            {
                _context.SaveChanges();
            }
        }
    Sunday, December 20, 2015 12:18 PM

Answers