2012-09-24 23 views
5

リポジトリをEF5に更新しようとしましたが、いくつかのエラーが発生しました。私は類似のエラーのためにいくつかの質問/答えを発見したstackoverflowの周りを見てきましたが、残念ながら同じ答えが私の問題を解決しませんでした。エンティティタイプはモデルの一部ではありません。EF 5

これは私のエラーです:

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

大歓迎任意のヘルプ:)

答えて

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

感謝を解決するためである

、私がやって試してみましたこれは今、私は同じエラーが発生します。私はもともとあなたのコードを追加しましたが、VSはDbContextからOnModelCreatingをオーバーライドしていないと言っていましたので、仮想をオーバーライドに置き換えました。これを実行した後、私は 'modelBuilder.Entity ();'行にブレークポイントを追加しましたが、ブレークポイントにヒットしなかったのに同じエラーが返されました。 –

+1

@SHTesterローカルのASP.NET開発サーバー/ IISを停止して起動してみてください。 – Eranga

+1

これまでと同じエラーが発生しましたが、マシンを再起動します。 –

3

私はEF 5と同じ問題があったすべてのものが再び働いていた... ... TTファイルで作成されたすべてのファイルを削除するには、アイブ氏は何をしにして、再びそれらを再作成します!

0

あなたのテーブルモデル名は間違っています。 Sqlサーバーにsatesがある場合は正しい名前を設定します。 戻るビュー(db.tblEmployees.ToList()); エラー行.....

行くモデルとduble(tblEmployees)のようなテーブル名と一致するよりも、ウルモデル名をクリックし、エラーが助けを

関連する問題