2013-07-12 64 views
0

我使用EF 5.0和Code First。在我的通用版本庫中,我有一種邏輯方式排除記錄的方法。此方法實際上執行更新,將實體字段的狀態設置爲false。實體框架篩選器邏輯刪除記錄

我想攔截我的查詢,並只篩選status == true的地方。

有沒有簡單的方法來做到這一點?例如:

new GenericRepository<Entity>().ToList(); 
// and internally it will filter where status == true. 

回答

2

創建一個通用的方法

public IQueryable<T> All<T>(Expression<Func<T, bool>> predicate) { 

    return context.Set<T>().Where(predicate); 

} 

,如果你想要更多的東西鏈接到您的status財產,你必須使用反射,並通過自己構建拉姆達(因爲你不能使用接口linq來實現查詢)。

類似的東西(未經測試),調用通用的All方法。

public IQueryable<T>AllButDeleted<T>() { 
    var property = typeof(T).GetProperty("status"); 
    //check if T has a "status" property 
    if (property == null && || property.PropertyType != typeof(bool)) throw new ArgumentException("This entity doesn't have an status property, or it's not a boolean"); 
    //build the expression 
    //m => 
     var parameter = new ParameterExpression(typeof(T), "m"); 
    // m.status 
    Expression body = Expression.Property(parameter, property); 
    //m.status == true (which is just m.status) 
    body = Expression.IsTrue(body); 
    //m => m.status 
    var lambdaPredicate = Expression.Lambda<Func<T, bool>>(body, new[]{parameter}); 
    return All(lambdaPredicate); 
    } 
+0

謝謝!這就是我一直在尋找的。 –

2

你可以讓你所有的實體實現一些IDeletable接口:

public interface IDelitable 
{ 
    bool IsDeleted { get; } 
} 

並添加約束到存儲庫

public class GenericRepository<T> 
    where T: class, IDelitable 

的泛型參數並添加過濾器,當你返回值:

context.Set<T>().Where(e => !e.IsDeleted) 
+0

@RaphaëlAlthaus不,它工作得很好。因爲它不知道任何關於實體的接口 - 它與實現一起工作。 –

+0

@RaphaëlAlthaus我完全相信它不會,因爲剛剛使用EF5驗證了這個代碼:) –

+1

好了,那麼,我將不得不重試,thx進行測試;) –

0

您可以使用Where過濾它。

​​