[英]Using same query for different DbSets in Entity Framework
I have two different views to the same database table and I would like to use same query against both views. 我对同一数据库表有两个不同的视图,并且我想对两个视图使用相同的查询。
I have now following ugly solution where I have basically copy of the query. 我现在有以下丑陋的解决方案,其中我基本上有查询的副本。
DbSet<uvw_MyView1> view1 = entities.uvw_MyView1;
DbSet<uvw_MyView2> view2 = entities.uvw_MyView2;
if(doQueryOnView1)
{
// huge query
view1.Where(jada jada)
}
else
{
// huge query copied here with exception of different DbSet
view2.Where(jada jada)
}
What I would like to have is more elegant solution. 我想拥有的是更优雅的解决方案。 Basically I want to have same query part for two DbSet's that are in column wise exactly same, but those return different rows. 基本上,我想对按列方式完全相同的两个DbSet具有相同的查询部分,但是它们返回不同的行。 Any ideas? 有任何想法吗?
Add interface containing all properties of your view and implement this interface by both view entities. 添加包含视图的所有属性的接口,并通过两个视图实体实现此接口。 After that create method containing whole your query: 在包含整个查询的create方法之后:
public static IQueryable<T> YourHugeQueryMethod<T>(DbContext context, ...)
where T : class, IView, new()
{
DbSet<T> set = context.Set<T>();
return set.Where(...);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.