我开始实现UnitOfWork和Repository,我有一个模型,它显示了我的数据库,我创建了一个文件夹VIEWMODELS,我添加了我需要的模型,但是当我想根据我的VIEWMODEL类进行查询时,我得到以下错误:< / p>
实体类型FaultStatViewModel不是当前上下文模型的一部分。有什么建议吗?
这是我的存储库:
public class GenericRepositoryTest<TEntity> where TEntity:class
{
internal LTWCentralEntities context;
internal DbSet<TEntity> dbSet;
public GenericRepositoryTest(LTWCentralEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
}
上面的代码与Microsoft文档中的代码完全相同,以下是我的UnitOfWork:
private CentralEntities DB = new CentralEntities();
private GenericRepositoryTest<FaultStatViewModel> statTest;
public GenericRepositoryTest<FaultStatViewModel> genecricReposityTurbine
{
get
{
return this.statTest ?? new GenericRepositoryTest<FaultStatViewModel>(DB);
}
}
在我的控制器中,我编写了以下代码:
UnitOfWorkTest unitTest = new UnitOfWorkTest();
var crs = unitTest.genecricReposityTurbine.Get(filter: f => f.m_error_group_name.Equals("COMMUNICATION ERROR"));
这是我收到错误的地方:
public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
}