C# 基于实体框架6.1和SQLServerCE4异常的通用存储库。“无效操作例外”
我正试图创建一个基于EntityFramework6.1和SQLServerCE4的通用存储库,但有一个例外 “System.InvalidOperationException”类型的第一次意外异常 发生在EntityFramework.dll中 附加信息:实体类型TestClass不是 当前上下文的模型 这是我存储库的代码,我还附加了一个测试项目C# 基于实体框架6.1和SQLServerCE4异常的通用存储库。“无效操作例外”,c#,entity-framework,C#,Entity Framework,我正试图创建一个基于EntityFramework6.1和SQLServerCE4的通用存储库,但有一个例外 “System.InvalidOperationException”类型的第一次意外异常 发生在EntityFramework.dll中 附加信息:实体类型TestClass不是 当前上下文的模型 这是我存储库的代码,我还附加了一个测试项目 public class Repository<T> : IRepository<T> where T : class {
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();
}
}
初始化
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; }
}
我假设您首先使用的是EF代码,并且DataContext类中没有定义DbSet属性 对不起,目前无法查看您的TestApp