使得实体框架全局筛选器 [英] making a global filter for entity framework

查看:124
本文介绍了使得实体框架全局筛选器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的模型我对所有的有效属性,我希望过滤所有非活动如果在管理中不显示模式
什么是做的最好的方法,我目前使用的是以下

For my models I have a active attribute on all of them, and i want to filter all inactive if the model was not displayed on the administration What is the best way to do that, What I'm currently using is the following

在我的基础模型类我有这样的方法筛选集合

in my base model class i have this method that filters the collections

public virtual IQueryable<T> GlobalDefaultScope<T>(IQueryable<T> c) where T : CModel<T>
{
    if (settings.is_admin)
    {
        c = c.Where(m => m.active);
    }
    return c;
}

和我的模型,每相对于我做了以下方法

and on my model for every relation i did the following method

DbSet<T> set ...
var X = set.Where(some filter);
var list = globalDefaultScope(X).ToList();
return list;

而现在我有,当我想用​​包括(Xmodel.Ymodel)我叫 globalDefaultScope 在该集合过滤收集 GET 方法,但它一直在收藏中有一些项目是不活跃抛出此异常。

And now I'm having some serious problems when i want to eagerly load some subrelations using include("Xmodel.Ymodel") i called the globalDefaultScope in the get method for that collection that filters the collection but it keeps throwing this exception when some items in the collection are inactive

System.InvalidOperationException:
    操作失败:关系不能被改变,因为一个或多个
    外键的属性是不可为空。

System.InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable.

我怎样才能解决这个或我怎么可以在一个更优雅的方式,因为我真的不是很满意的我是如何实现它这个过滤器。

how can i fix this or how can i make this filter in a more elegant way because i'm really not very satisfied of how i implemented it.

请向所有缺少的信息或code块或任何细节

please ask for Any missing information or code blocks or any details

更新:

我也发现了这个<一个href=\"http://www.matthidinger.com/archive/2012/01/25/a-smarter-infrastructure-automatically-filtering-an-ef-4-1-dbset.aspx\"相对=nofollow>链接,但这种方式并没有与即时加载项工作(包括()

I also found this link, but this way didn't work with eagerly loaded entries (include())

UPDATE2:

这是我如何使用包括和一个示例,其中发生错误

this is an example of how i use the include and where the error occurs

在我的模型

public IQueryable<Dish> getSomeRelation(bool eagerly_load_sub_relation1, bool eagerly_load_sub_relation2)
        {
            var query = getQuery(...);
            //getQuery =>  query = db.Entry(obj).Collection(collection).Query() 
            //GlobalDefaultScope(query)
            if ( eagerly_load_sub_relation1){
                query = query.Include(m => m.sub_relation1);
            }
            if (eagerly_load_sub_relation2){
                query = query.Include("sub_relation2.sub_relation_of_sub_relation2");
            }
            return query;
        }

而在包括我做了以下我不能过滤关系:

while i couldn't filter the relations in the include i did the following :

private ICollection<SubRelation1> _sub_relation1 {get; set;}
public ICollection<SubRelation1> sub_relation1 {
get 
{
   //something like:
   return GlobalDefaultScope(_sub_relation1 ).ToList(); 
}  
set;}

而我们筛选sub_relation1的结果,当我这样做db.SaveChanges()所提到的错误被抛出。

while we filter the results in sub_relation1, when i do db.SaveChanges() the mentioned error is thrown.

推荐答案

您可以创建扩展方法,如

You can create extension methods like

public static IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate)
{
    source = source.Where("isActive == true"); // From System.linq.Dynamic Library
    source = Queryable.Where<T>(source, predicate);
    return source;
}

和使用它们像

var user = db.UserProfiles.Include("webpages_Roles").Where(u => u.UserId < 30);

这应该工作,请让我知道如果你需要更多的信息。

This should work, Please Let me know if you need more information.

注意:请添加的NuGet包System.Linq.Dynamic有动态的LINQ库

更新1:

我在webpages_Roles和简单的会员UsreProfile表包括IsActive,也包括多了一个表的角色优先是指webpages_Roles,为方便使用,我已经改变了所有的关系,船舶一对多(从多对多) 。现在,如果我用类似你们code,扩展方法的时候有任何用户对其中有没有角色给了错误。

I have included IsActive in webpages_Roles and UsreProfile table of simple membership and also included one more table Role Priority which refers to webpages_Roles, for easy to use I have changed the all the relation ships to one to many(from many to many). now if I use code similar to yours, extension method gives error when there is any user for which there are no roles.

如果我给角色,每一个用户,我对每一个角色的优先级,那么这将不会给错误。

If I give roles to every user and I have priority for every roles then this won't give error.

请让我知道如果你还需要其他信息。

Please Let me know if you still need any other info.

更新2

您可以创建一个像

public static IQueryable<T> WhereActive<T>(this IQueryable<T> source)
        {
            return source.Where("isActive == true"); // From System.linq.Dynamic Library
        }

和调用其他方法,如

var user = db.UserProfiles.Include("webpages_Roles").WhereActive().Where(u => u.UserId < 30);

var user = db.UserProfiles.Include("webpages_Roles").WhereActive().FirstOrDefault(u => u.UserId < 30);

请让我知道如果你还需要其他信息。

Please Let me know if you still need any other info.

这篇关于使得实体框架全局筛选器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆