0
我正在使用EF 4和存储库模式。在尝试单元测试EF数据上下文的冒险中,我遇到了ADO.Net模拟上下文生成器模板,正如其名称所述,它有助于模拟数据上下文。ADO.Net模拟上下文生成器接口问题
虽然我的上下文确实没问题,但我的存储库存在问题,这有点神秘。这是我的资料库界面(略):
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认为一切都很好(即在界面下没有箭头,告诉我需要实施它)。
下面是通过我的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没有实现时,它显然是。
这可能只是一个黑暗中的镜头,但尝试显式实现。 – Divi 2011-02-18 06:02:26