Sto tentando di aggiornare il mio repository su EF5 ma ho riscontrato alcuni errori. Ho dato un'occhiata a StackOverflow per errori simili scoperto alcune domande/risposte ma sfortunatamente le stesse risposte non hanno risolto il mio problema.Il tipo di entità non fa parte del modello, EF 5
Questo è il mio errore:
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.
Questa è la mia classe DbContext:
public abstract class WebModelContext : DbContext
{
public WebModelContext()
: base("WebConnection")
{
Configuration.LazyLoadingEnabled = true;
}
}
Questa è la mia classe di contesto che eredita la mia WebModelContext
classe:
public class AccountContext : WebModelContext
{
private DbSet<User> _users;
public AccountContext()
: base()
{
_users = Set<User>();
}
public DbSet<User> Users
{
get { return _users; }
}
}
Questo è il mio classe repository:
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;
}
}
}
}
Questa è la mia classe 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;
}
}
Qualsiasi aiuto molto apprezzato :)
Grazie per l'aiuto, ho provato a fare questo solo ora e incontro lo stesso errore. Originariamente ho aggiunto il tuo codice, ma VS ha detto che non stava sovrascrivendo OnModelCreating da DbContext, quindi ho sostituito virtuale con override. Dopo averlo fatto ho aggiunto un breakpoint alla riga 'modelBuilder.Entity();' ma non ha raggiunto il punto di interruzione e ha comunque generato lo stesso errore. –
@SHTester Provare a fermare e avviare il server di sviluppo ASP.NET locale/IIS – Eranga
okay, fatto lo stesso errore, proverò a riavviare il computer. –