2012-09-24 19 views
5

Próbuję zaktualizować moje repozytorium do wersji EF5, ale napotkałem kilka błędów. Rozejrzałem się wokół stackoverflow dla podobnych błędów odkryłem kilka pytań/odpowiedzi, ale niestety te same odpowiedzi nie rozwiązały mojego problemu.Typ jednostki nie jest częścią modelu, EF 5

To jest mój błąd:

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. 

To moja klasa DbContext:

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

To moja klasa kontekst, który dziedziczy mój WebModelContext Klasa:

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

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

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

To jest mój Klasa repozytorium:

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

} 

To mój AccountRepository klasa:

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

Każda pomoc mile widziana :)

Odpowiedz

6

EF może nie być w stanie odebrać typów jednostek zostały zadeklarowane. Zastąp element OnModelCreating i dodaj do niego jednostkę 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

Dzięki za pomoc, próbowałem robić to właśnie teraz i spotykam ten sam błąd. Pierwotnie dodałem twój kod, ale VS powiedział, że nie przesłania OnModelCreating z DbContext, więc zastąpiłem wirtualny nadpisaniem. Po wykonaniu tego dodałem punkt przerwania do linii 'modelBuilder.Entity ();' ale nie trafił punktu przerwania i nadal dawał ten sam błąd. –

+1

@SHTester Spróbuj zatrzymać i uruchomić lokalny program ASP.NET Development Server/IIS – Eranga

+1

OK, wykonałem ten sam błąd, spróbuję ponownie uruchomić komputer. –

3

miałem ten sam problem z EF 5 ... Co Ive done było usunąć wszystkie pliki utworzone przez pliki TT i odtworzyć je ponownie ... wszystko było znów działa!

0

Twoja nazwa modelu stołu jest nieprawidłowa. ustawić poprawną nazwę, gdy sates w serwerze Sql. return View (db.tblEmployees.ToList()); linia błąd .....

przejdź modelu i duble kliknij ur nazwę modelu niż dopasowanie z nazwy tabeli jak (tblEmployees)

Twój błąd to rozwiązać

Powiązane problemy