6

I am trying to update my repository to EF5 but have encountered a few errors. I've taken a look around stackoverflow for similar errors discovered a few questions/answers but unfortunately the same answers didn't resolve my issue.

This is my error:

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 greatly appreciated :)

Michael
  • 421
  • 2
  • 10
  • 23

3 Answers3

6

EF may not be able to pick up the entity types you have declared. Override the OnModelCreating and add the User entity to 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>();
    }
}
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • Thanks for the help, I have tried doing this just now and I encounter the same error. I originally added your code but VS said it wasn't overriding the OnModelCreating from DbContext so I replaced virtual with override. After doing that I added a breakpoint to the line `modelBuilder.Entity();` but it didn't hit the breakpoint and still gave the same error. – Michael Sep 24 '12 at 08:43
  • 1
    @SHTester Try stopping and starting local ASP.NET Development Server/IIS – Eranga Sep 24 '12 at 09:02
  • 1
    okay have done, still the same error, i'll try rebooting the machine. – Michael Sep 24 '12 at 09:18
  • Amazing! This really should help. Is there another User class hanging around somewhere? – Gert Arnold Sep 25 '12 at 10:14
  • Hi, Eranga! What might be the cause for "EF may not be able to pick up the entity types you have declared"? I thought, that having DbSet property should add entity to model... – Prokurors Jun 10 '15 at 13:47
3

I have had the same problem with EF 5... What Ive done was to delete all files created by tt files and re-create them again... every thing was working again!

Gabriel
  • 47
  • 1
0

Your table model name is wrong. set the correct name when sates in Sql server. return View(db.tblEmployees.ToList()); error line.....

go Model and duble click ur model name than matching with table name like (tblEmployees)

your error is solve