Entity framework 实体框架筛选逻辑删除记录
我首先使用EF5.0和代码。在我的通用存储库中,我有一个以逻辑方式排除记录的方法。此方法实际上执行更新,将实体字段的状态设置为false 我想截取我的查询,只在status==true的地方进行过滤 有没有简单的方法?例:Entity framework 实体框架筛选逻辑删除记录,entity-framework,entity-framework-5,Entity Framework,Entity Framework 5,我首先使用EF5.0和代码。在我的通用存储库中,我有一个以逻辑方式排除记录的方法。此方法实际上执行更新,将实体字段的状态设置为false 我想截取我的查询,只在status==true的地方进行过滤 有没有简单的方法?例: new GenericRepository<Entity>().ToList(); // and internally it will filter where status == true. newgenerirepository().ToList(); /
new GenericRepository<Entity>().ToList();
// and internally it will filter where status == true.
newgenerirepository().ToList();
//在内部,它将过滤status==true的位置。
您可以让您的所有实体实现一些IDeletable
接口:
public interface IDelitable
{
bool IsDeleted { get; }
}
并向存储库的泛型参数添加约束
public class GenericRepository<T>
where T: class, IDelitable
公共类通用存储库
式中T:类,可理想
并在返回值时添加筛选器:
context.Set<T>().Where(e => !e.IsDeleted)
context.Set()。其中(e=>!e.IsDeleted)
您可以让您的所有实体实现一些IDeletable
接口:
public interface IDelitable
{
bool IsDeleted { get; }
}
并向存储库的泛型参数添加约束
public class GenericRepository<T>
where T: class, IDelitable
公共类通用存储库
式中T:类,可理想
并在返回值时添加筛选器:
context.Set<T>().Where(e => !e.IsDeleted)
context.Set()。其中(e=>!e.IsDeleted)
您可以使用Where对其进行过滤
.Where(e => e.Status == true).ToList();
您可以使用Where对其进行过滤
.Where(e => e.Status == true).ToList();
创建泛型方法
public IQueryable<T> All<T>(Expression<Func<T, bool>> predicate) {
return context.Set<T>().Where(predicate);
}
创建泛型方法
public IQueryable<T> All<T>(Expression<Func<T, bool>> predicate) {
return context.Set<T>().Where(predicate);
}
@RaphaëlAlthaus不,它很好用。因为它对实体的接口一无所知——它与实现一起工作。@RaphaëlAlthaus我完全相信它不会,因为它只是用EF5验证了这段代码:)@RaphaëlAlthaus不,它工作得很好。因为它对实体的接口一无所知——它与实现一起工作。@RaphaëlAlthaus我完全相信它不会,因为它刚刚用EF5验证了这段代码:)