2011-02-18 43 views
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沒有實現時,它顯然是。

+0

這可能只是一個黑暗中的鏡頭,但嘗試顯式實現。 – Divi 2011-02-18 06:02:26

回答

0

原來我有一個對我的倉庫中的另一組實體的引用,以及對我的模擬數據項目中不同集合的引用。