在EF上具有默认过滤器的实体 [英] Entity with default filter on EF

查看:120
本文介绍了在EF上具有默认过滤器的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.edmx文件上有一个名为Client的实体。

I have an entity called Client on my .edmx file.

我必须再次运行多个linq查询,但在所有这些查询上,我都需要一个过滤器(可以说active = 1)。

I have to run several linq querys agains it but on all of them, i need a filter (lets say active=1).

我不想在所有的c.active == 1 上使用我的查询对我的实体应用了默认过滤器更有意义。

I dont want to have a where c.active == 1 on all my queries, it makes more sense that my entity has a default filter applied. Is it something that can be done?

或者也许我可以将查询作为实体而不是表的基础? (我使用数据库的第一种方法)

Or maybe can I have a query as base to an entity instead of a table? (I'm on a database first approach)

我知道另一种解决方案是在数据库上创建视图并将实体链接到该视图,但是我不这样做

I know another solution would be to create a view on the DB and link the entity to the view, but I don't want that either.

推荐答案

您可以在生成的数据库上下文中添加其他方法:

You may add additional method to your generated Database context:

partial class DatabaseContext // same name as your generated context
{
    IQueryable<Client> ActiveClients { get { return Clients.Where(c => c.active == 1); } }
}

并在整个代码中使用它。由于它是部分类的一部分(在生成的上下文类以外的其他文件中定义),因此刷新架构时,它不受代码生成工具的影响。

And use it throughout your code. As it's part of partial class (defined in some other file than your generated context class), it's not affected by codegen tool when you refresh your schema.

这篇关于在EF上具有默认过滤器的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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