EF 6 过滤子集合

EF 6 filtering child collections(EF 6 过滤子集合)
本文介绍了EF 6 过滤子集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将旧项目从 Linq2Sql 迁移到 EF6,但遇到以下问题.

I'm trying to migrate old project from Linq2Sql to EF6 and I got following issue.

这个项目是多语言的(即所有文本都有不止一种翻译),我有以下数据库结构:

This project is multilingual (i.e. all texts have more than 1 translation) and I have following db structure:

获取所有 ExampleEntity1 对象以及所有按当前语言 ID 过滤的 LocalizedContent 记录的最佳方法是什么?

What is the best way to get all ExampleEntity1 objects with all LocalizedContent records filtered by current language id?

我可以使用以下代码加载所有带有所有 LocalizedContent 记录的 ExampleEntity1 对象:dc.ExampleEntity1.Include(ee => ee.TextEntry.LocalizedContents);

I can load all ExampleEntity1 objects with all LocalizedContent records using following code: dc.ExampleEntity1.Include(ee => ee.TextEntry.LocalizedContents);

在 Linq2Sql 中,我可以使用 loadOptions.AssociateWith过滤 LocalizedContent 记录,但我找不到任何适用于 EF6 的解决方案.

In Linq2Sql I can filter LocalizedContent records using loadOptions.AssociateWithbut I can't find any solution for EF6.

我看到了类似的旧问题(2-3 年前发布),我只是想知道是否有适用于 EF6 的解决方案.这对我来说是一个非常关键的功能,因为我在项目中有几十个实体,我不想为每个选择查询创建自定义对象.

I saw similar old questions (posted like 2-3 years ago) and I'm just wondering if there is a solution for EF6. It is a very critical feature for me because I have dozens of entities in the project and I don't want to create custom objects for each select query.

我还发现 EntityFramework.DynamicFilters nuget 包可以帮助解决我的问题,但如果可能的话,我更愿意使用本机"EF6 功能..

I also found EntityFramework.DynamicFilters nuget package which can help with my issue, but I would prefer to use "native" EF6 functionality if possible..

推荐答案

如果你想在对数据库的查询中执行过滤,那么(从 EF6 开始)你必须使用 查询方法:

If you want to perform the filtering in the query to the database then (as of EF6) you have to use the Query method:

Query 方法提供对实体框架在加载相关实体时将使用的底层查询的访问.然后,您可以使用 LINQ 将过滤器应用于查询,然后通过调用 LINQ 扩展方法(例如 ToList、Load 等)来执行查询.

The Query method provides access to the underlying query that the Entity Framework will use when loading related entities. You can then use LINQ to apply filters to the query before executing it with a call to a LINQ extension method such as ToList, Load, etc.

using (var context = new BloggingContext()) 
{ 
  var blog = context.Blogs.Find(1); 

  // Load the posts with the 'entity-framework' tag related to a given blog 
  context.Entry(blog) 
    .Collection(b => b.Posts) 
    .Query() 
    .Where(p => p.Tags.Contains("entity-framework") 
    .Load(); 

   // Load the posts with the 'entity-framework' tag related to a given blog  
   // using a string to specify the relationship  
   context.Entry(blog) 
     .Collection("Posts") 
     .Query() 
     .Where(p => p.Tags.Contains("entity-framework") 
     .Load(); 
}

但是,明显的缺点是您必须对每个条目执行此操作,并且每个 Load 调用都会对数据库执行一次查询.

However, the obvious drawback is that you have to do this per entry and each Load call executes a query against the database.

除非这对您来说是一个硬性要求,否则我会选择仅加载所有本地化并简单地在内存中过滤以使用所选语言.我很确定性能不会成为问题.

Unless it's a hard requisite for you I would opt for just loading all the localizations and simply filter in memory to use the selected language one. I'm pretty sure the performance won't be an issue.

这篇关于EF 6 过滤子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)