实体类型不属于模型,EF 5。

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

任何帮助都非常感激 :)
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>();
    }
}

谢谢您的帮助,我刚刚尝试了这个方法,但是遇到了相同的错误。我最初添加了您的代码,但VS说它没有覆盖DbContext中的OnModelCreating,所以我用override替换了virtual。在那之后,我在modelBuilder.Entity<User>();这一行上添加了断点,但它没有触发断点,仍然出现了相同的错误。 - Michael
1
@SHTester 请尝试停止并启动本地的ASP.NET开发服务器/IIS。 - Eranga
1
好的,我已经完成了,但是还是出现同样的错误,我会尝试重新启动机器。 - Michael
太棒了!这真的会有所帮助。还有其他的用户类吗? - Gert Arnold
嗨,Eranga!“EF可能无法获取您声明的实体类型”的原因是什么?我认为,拥有DbSet属性应该将实体添加到模型中... - Prokurors

3

我曾经遇到过与EF 5相关的同样问题,我的解决方法是删除所有由tt文件创建的文件,然后重新创建它们...这样一切就恢复正常了!


0

您的表模型名称有误。在连接到 Sql Server 时,请设置正确的名称。

返回视图时使用以下代码:return View(db.tblEmployees.ToList()); 错误行.....

请前往模型并双击模型名称,然后将其与表名匹配,例如(tblEmployees)。

这样就可以解决您的错误了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接