Object type is not part of the model, EF 5

I am trying to upgrade my repository to EF5, but have encountered several errors. I looked through stackoverflow for similar errors, found several questions / answers, but unfortunately the same answers did not solve my problem.

That's my fault:

The entity type User is not part of the model for the current context. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

This is my DbContext class:

 public abstract class WebModelContext : DbContext { public WebModelContext() : base("WebConnection") { Configuration.LazyLoadingEnabled = true; } } 

This is my context class that inherits my WebModelContext class:

 public class AccountContext : WebModelContext { private DbSet<User> _users; public AccountContext() : base() { _users = Set<User>(); } public DbSet<User> Users { get { return _users; } } } 

This is my repository class:

 public abstract class IRepository<T> : IDisposable where T : WebModelContext, new() { private T _context; protected T Context { get { return _context; } } public IRepository() { _context = new T(); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~IRepository() { Dispose(false); } protected virtual void Dispose(bool disposing) { if (disposing) { if (_context != null) { _context.Dispose(); _context = null; } } } } 

This is my AccountRepository class:

 public class AccountRepository : IRepository<AccountContext> { public List<User> GetUsers() { return Context.Users.ToList(); } public User GetUser(string username) { return Context.Users.Where(u => u.Name == username).FirstOrDefault(); } public User CreateUser(string username, string password, string salt, int age, int residence) { User user = new User { Name = username, Password = password, Salt = salt, RoleId = 1, CreatedOn = DateTime.Now, Locked = false, Muted = false, Banned = false, Guid = Guid.NewGuid().ToString("N") }; Context.Users.Add(user); return Context.SaveChanges() > 0 ? user : null; } } 

Any help is much appreciated :)

+5
entity-framework-5 entity-framework dbcontext
Sep 24
source share
3 answers

EF may not be able to select the types of objects that you declared. Override OnModelCreating and add the User object to the model.

 public class AccountContext : WebModelContext { private DbSet<User> _users; public AccountContext() : base() { _users = Set<User>(); } public DbSet<User> Users { get { return _users; } } protected virtual void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<User>(); } } 
+6
Sep 24
source share
β€” -

I had the same problem with EF 5 ... What did I do to delete all the files created by tt files and create them again again ... everything worked again!

+3
Oct 08
source share

Incorrect name for your table. set the correct name if indicated on the Sql server. return View (db.tblEmployees.ToList ()); error string

go Model and duble click ur model name instead of matching with table name e.g. (tblEmployees)

your mistake is resolved.

0
Aug 07 '15 at 10:13
source share



All Articles