实体类型不是模型EF 5的一部分 [英] The entity type is not part of the model, EF 5

查看:107
本文介绍了实体类型不是模型EF 5的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的错误:

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类:

This is my DbContext class:

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

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

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

这是我的存储库类:

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类:

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

任何帮助表示赞赏:)

推荐答案

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

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

这篇关于实体类型不是模型EF 5的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆