2012-09-24 14 views
5

Veri havuzumu EF5'e güncellemeye çalışıyorum ancak birkaç hatayla karşılaştım. Birkaç soru/cevap bulduğu benzer hatalar için stackoverflow etrafına bir göz attım ama maalesef aynı cevaplar sorunumu çözmedi.Varlık türü, modelin bir parçası değil EF 5

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

Bu benim WebModelContext sınıfını miras benim bağlam sınıftır: Bu benim DBContext sınıftır

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. 

:

Bu

benim hatadır

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

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

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

Bu benim depo sınıfı:

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

} 

Bu benim AccountRepository sınıftır: büyük takdir

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

Herhangi bir yardım :)

cevap

6

EF, beyan ettiğiniz varlık türlerini almak mümkün olmayabilir. OnModelCreating'u geçersiz kılın ve modele User nesnesini ekleyin.

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

Teşekkür çözmek olduğunu (TblEmployees) gibi tablo adı ile eşleşen daha ur model adını tıklayın yaptığım denedi Bu şimdi ve aynı hatayla karşılaşıyorum. Aslında kodunuzu ekledim ama VS, DbContext'ten OnModelCreating'i geçersiz kılmadığını, böylece sanal olarak geçersiz kılmayı değiştirdiğimi söyledi. Bunu yaptıktan sonra 'modelBuilder.Entity ();' satırına bir kesme noktası ekledim, ancak kesme noktasını vurmadı ve hala aynı hatayı verdi. –

+1

@SHTester Yerel ASP.NET Geliştirme Sunucusu/IIS – Eranga

+1

'u durdurmayı ve başlatmayı tamamlamayı deneyin, hala aynı hatayı, makineyi yeniden başlatmayı deneyeceğim. –

3

Ben bitirdim Ive ... Her şey yine çalışıyordu tt dosyaları tarafından oluşturulan tüm dosyaları silmek ve bunları yeniden yaratmaktı Ne ... EF 5 ile aynı problem vardı!

0

Tablo model adınız yanlış. Sql sunucuda satirlarinda dogru adi ayarlayin. dönüş Görünümü (db.tblEmployees.ToList()); hata hattı .....

go Modeli ve duble

Hata yardım

İlgili konular