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

私はEF5で同じ問題を抱えていました...私がしたことは、ttファイルによって作成されたすべてのファイルを削除し、それらを再作成することでした...すべてが再び機能していました!

于 2012-10-08T19:36:59.330 に答える
0

テーブルのモデル名が間違っています。Sqlサーバーでsatesするときに正しい名前を設定します。ビューを返します (db.tblEmployees.ToList()); エラー行.....

モデルに移動し、(tblEmployees)のようなテーブル名と一致するよりもモデル名をダブルクリックします

あなたのエラーは解決しました

于 2015-08-07T10:13:13.207 に答える