6

我正在尝试将我的存储库更新到 EF5,但遇到了一些错误。我查看了 stackoverflow 的类似错误,发现了一些问题/答案,但不幸的是,相同的答案并没有解决我的问题。

这是我的错误:

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.

这是我的 DbContext 类:

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

这是继承我的类的上下文WebModelContext类:

public class AccountContext : WebModelContext
{
    private DbSet<User> _users;

    public AccountContext()
        : base()
    {
        _users = Set<User>();
    }

    public DbSet<User> Users
    {
        get { return _users; }
    }
}

这是我的存储库类:

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;
            }
        }
    }

}

这是我的 AccountRepository 类:

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;
    }
}

非常感谢任何帮助:)

4

3 回答 3

6

EF 可能无法获取您声明的实体类型。覆盖OnModelCreating并将User实体添加到模型。

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>();
    }
}
于 2012-09-24T08:33:13.617 回答
3

我在使用 EF 5 时遇到了同样的问题……我所做的是删除所有由 tt 文件创建的文件并重新创建它们……一切都恢复正常了!

于 2012-10-08T19:36:59.330 回答
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

于 2015-08-07T10:13:13.207 回答