Answered by:
IGenericRepository<T> - Err The entity type Employee_Data is not part of the model for the current context ?

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
-
>>"did I must add something in OnModelCreating because I'm using code first" ?
You need to tell the DbContext about the tables to be created. Put the following code in your DatabaseContext class as suggested here: http://stackoverflow.com/questions/20688922/the-entity-type-type-is-not-part-of-the-model-for-the-current-context
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Employee_Data>().ToTable("Employee_Data"); }
Please refer to the following links for more information about table mappings:
http://stackoverflow.com/questions/9549828/dbcontext-table-mappings
https://msdn.microsoft.com/en-us/data/jj591617.aspx?f=255&MSPPError=-2147217396Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
Sunday, December 20, 2015 5:04 PM