【发布时间】:2014-03-20 10:07:43
【问题描述】:
我已将服务添加到我的 MVC/EF 项目中,以充当我的控制器和存储库之间的层。
我在将方法从存储库复制到服务时遇到问题。我正在尝试在服务中使用Count(),但不断收到错误does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type .. could be found
我的实现方式与存储库完全相同,所以我不知道它为什么会失败
存储库:
public abstract class Repository<CEntity, TEntity> : IRepository<TEntity> where TEntity : class
where CEntity : DbContext, new()
{
private CEntity entities = new CEntity();
protected CEntity context
{
get { return entities; }
set { entities = value; }
}
public virtual int Count
{
get { return entities.Set<TEntity>().Count(); }
}
public virtual IQueryable<TEntity> All()
{
return entities.Set<TEntity>().AsQueryable();
}
}
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
int Count { get; }
IQueryable<TEntity> All();
}
服务:
public class CampaignService : ICampaignService
{
private readonly IRepository<Campaign> _campaignRepository;
public CampaignService(IRepository<Campaign> campaignRepository)
{
_campaignRepository = campaignRepository;
}
public int Count
{
get { return _campaignRepository.Count()**; }
}
public IQueryable GetAll()
{
return _campaignRepository.All();
}
}
public interface ICampaignService
{
int Count{ get; }
IQueryable GetAll();
}
** 它在这一行失败。
`错误 4“MarketingSystem.Repositories.Common.IRepository”不包含“Count”的定义,并且找不到接受“MarketingSystem.Repositories.Common.IRepository”类型的第一个参数的扩展方法“Count”(您是否缺少 using 指令或程序集引用?)
GetAll()/All() 方法可以正常工作,但 Count() 不行。
谁能发现并解释我哪里出错了?
【问题讨论】:
-
尝试将
get { return _campaignRepository.Count()**; }更改为get { return GetAll().Count(); }。
标签: c# asp.net-mvc entity-framework compiler-errors