我正在使用EF 4和存储库模式。在尝试对我的EF数据上下文进行单元测试的冒险中,我遇到了ADO.Net Mocking Context Generator模板,正如其名称所描述的那样,它有助于模拟数据上下文。
虽然我的上下文确实很好,但我的存储库存在问题,这有点神秘。这是我的存储库界面(缩写):
public interface IRepository<T> where T : class
{
/// <summary>
/// Finds all entities
/// </summary>
/// <returns></returns>
IQueryable<T> Find();
/// <summary>
/// Find alls entities and loads included relational properties.
/// </summary>
/// <param name="includes">Relational properties to load.</param>
/// <returns></returns>
IQueryable<T> Find(params Expression<Func<T, object>>[] includes);
/// <summary>
/// Adds an entity to the repository.
/// </summary>
/// <param name="entity">Entity to add.</param>
void Add(T entity);
/// <summary>
/// Updates an entity in the repository.
/// </summary>
/// <param name="entity">Entity to update.</param>
void Update(T entity)...etc
使用模拟上下文,您将获得一组具有虚拟属性的新生成实体。为了模仿它们,我给了那些实体与真实实体相同的命名空间,它们与我的实体相同,只有对模拟实体的引用。
问题是当我有我的存储库接口的实现时,我得到了接口未实现的异常,即使我实际上是在实现接口。这只发生在我尝试构建项目时。 Intellisense认为一切都很好(即界面下没有litte箭头告诉我需要实现它)。
下面是通过我的ICategoryLocalized接口实现的接口(它继承自IRepository)。
public class CategoryLocalizedRepository : MockDatabaseRepository, ICategoryLocalizedRepository
{
#region Constructor
public CategoryLocalizedRepository(MockContext context)
: base(context)
{
}
#endregion
#region Data Methods
public IQueryable<CategoryLocalized> Find()
{
return Context.CategoryLocalized;
}
public IQueryable<CategoryLocalized> Find(params Expression<Func<CategoryLocalized, object>>[] includes)
{
return CreateObjectQuery<CategoryLocalized>(Context.CategoryLocalized, includes);
}
public CategoryLocalized Get(int id)
{
return Context.CategoryLocalized.SingleOrDefault(d => d.CategoryLocalizedID == id);
}
public CategoryLocalized Get(int id, params Expression<Func<CategoryLocalized, object>>[] includes)
{
IObjectSet<CategoryLocalized> query = CreateObjectQuery<CategoryLocalized>(Context.CategoryLocalized, includes);
return query.SingleOrDefault(d => d.CategoryLocalizedID == id);
}
public void Add(CategoryLocalized categoryLocal)
{
AddEntity<CategoryLocalized>(Context.CategoryLocalized, categoryLocal);
}
public void Update(CategoryLocalized categoryLocal)
{
UpdateEntity<CategoryLocalized>(Context.CategoryLocalized, categoryLocal);
}...etc
我不知道为什么编译器说明在接口(IRepository)没有在CategoryLocalizedRepository中实现。
答案 0 :(得分:0)
原来我在我的存储库中引用了另一组实体,并在我的模拟数据项目中引用了另一组。