none
Generic repository based on Entity Framework 6 .1 and Sql Server Ce 4 exception. “InvalidOperationException” RRS feed

  • Question

  • I'm trying to make a generic repository based on Entity Framework 6 .1 and Sql Server Ce 4, But I get an exception.

    A first chance exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll Additional information: The entity type TestClass is not part of the model for the current context.

    It is the code of my repository and Also I've attached a test project.

    public class Repository<T> : IRepository<T> where T : class
    {
        private readonly DataContext context;
    
        private DbSet<T> dbSet;
    
        public Repository(DataContext dataContext)
        {
            this.context = dataContext;
            this.dbSet = this.context.Set<T>();
        }
    
        public IQueryable<T> GetAll()
        {
            throw new NotImplementedException();
        }
    
        public IQueryable<T> FindAllBy(Expression<Func<T, bool>> predicate)
        {
            throw new NotImplementedException();
        }
    
        public T FindFirstBy(Expression<Func<T, bool>> predicate)
        {
            throw new NotImplementedException();
        }
    
        public void Add(T entity)
        {
            this.dbSet.Add(entity);
            this.context.SaveChanges();
        }
    
        public void Delete(T entity)
        {
            throw new NotImplementedException();
        }
    
        public void Update(T entity)
        {
            throw new NotImplementedException();
        }
    }
    Initialization
    public class Framework
    {
        public Framework()
        {
            DbConfiguration.SetConfiguration(new DbConfig());
    
            var databaseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test", "Database");
            Directory.CreateDirectory(databaseDirectory);
            var databasePath = Path.Combine(databaseDirectory, "Database.sdf");
            var sqlCeConnectionString = string.Format("Data Source={0}", databasePath);
    
            var dataContext = new DataContext(sqlCeConnectionString);
            this.Repository = new Repository<TestClass>(dataContext);
        }
    
        public IRepository<TestClass> Repository { get; set; }
    }
    TestApp

    Thursday, December 18, 2014 1:39 PM

Answers