EF 6过滤儿童系列 [英] EF 6 filtering child collections

查看:167
本文介绍了EF 6过滤儿童系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

此项目是多语言的(即所有文本都有1个以上的翻译)我有以下数据库结构:

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

< img src =https://i.stack.imgur.com/92uKf.pngalt =DB表示例>

什么是获取所有具有所有LocalizedContent记录的所有ExampleEntity1对象的最佳方式,以过滤当前语言id?

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在Linq2Sql中我可以使用 loadOptions.AssociateWith

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

c>但我找不到任何解决方案的EF6。

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

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

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..

推荐答案

p>如果要在数据库查询中执行过滤(从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:


查询方法提供对实体框架在加载相关实体时将使用的底层查询的访问。然后,您可以使用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.



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

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 call对数据库执行一个查询。

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过滤儿童系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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