简体   繁体   English

EF Core 6 的多级属性

[英]Multiple level properties with EF Core 6

After upgrading from .NET 5.0 to .NET 6.0 (including all the relative Microsoft libraries as Entity Framework), I started getting this error when executing a query to my database context using EF Core:从 .NET 5.0 升级到 .NET 6.0(包括作为实体框架的所有相关 Microsoft 库)后,我在使用 EF Core 对我的数据库上下文执行查询时开始收到此错误:

System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Query.NavigationBaseIncludeIgnored': The navigation 'MarkersTranslation.Marker' was ignored from 'Include' in the query since the fix-up will automatically populate it. System.InvalidOperationException:为警告“Microsoft.EntityFrameworkCore.Query.NavigationBaseIncludeIgnored”生成错误:导航“MarkersTranslation.Marker”被查询中的“包含”忽略,因为修复将自动填充它。 If any further navigations are specified in 'Include' afterwards then they will be ignored.如果之后在“包含”中指定了任何进一步的导航,那么它们将被忽略。 Walking back include tree is not allowed.走回包括树是不允许的。 This exception can be suppressed or logged by passing event ID 'CoreEventId.NavigationBaseIncludeIgnored' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.通过将事件 ID 'CoreEventId.NavigationBaseIncludeIgnored' 传递给 'DbContext.OnConfiguring' 或 'AddDbContext' 中的 'ConfigureWarnings' 方法,可以抑制或记录此异常。

This is the incriminated code:这是有罪的代码:

Tour tour = await _context.Tours.Include(mpt => mpt.MarkersPerTours)
                                .ThenInclude(mrk => mrk.Marker)
                                .ThenInclude(mrkProp => mrkProp.MarkersTranslations)
                                .ThenInclude(mrk => mrk.Marker)
                                .ThenInclude(mrkTp => mrkTp.Type)
                                .FirstOrDefaultAsync(t => t.Id == tourId);

So every Tour entity has a set of related Marker with their translations, but every Marker also have a Type .所以每个Tour实体都有一组相关的Marker及其翻译,但每个Marker也有一个Type

Microsoft Docs seems to approve the pattern I use for this query in order to include multiple levels. Microsoft Docs似乎批准了我用于此查询的模式,以便包含多个级别。

Which is now the correct way to obtain the same result with EF Core 6?现在哪个是使用 EF Core 6 获得相同结果的正确方法?

It's interesting that this restriction "Walking back include tree is not allowed" is not in EF core 6's breaking changes list .有趣的是,EF 核心 6 的重大更改列表中没有“不允许回退包含树”的限制。

The way to include everything you want while only walking the tree forward is this:在只向前走树的同时包含您想要的所有内容的方法是这样的:

Tour tour = await _context.Tours
    .Include(tour => tour.MarkersPerTours)
        .ThenInclude(mpt => mpt.Marker)
            .ThenInclude(mrk => mrk.MarkersTranslations)
    .Include(tour => tour.MarkersPerTours)
        .ThenInclude(mpt => mpt.Marker)
            .ThenInclude(mrk => mrk.Type)
    .FirstOrDefaultAsync(t => t.Id == tourId);

BTW, I changed the names of the range variables, IMO it's clearer, and more conventional, to let the names reflect their types, not the properties they're going to navigate to.顺便说一句,我更改了范围变量的名称,IMO 更清晰,更传统,让名称反映它们的类型,而不是它们要导航到的属性。

Configure warnings to ignore it as below配置警告以忽略它,如下所示

services.AddDbContext<YourContext>(options =>
        {
          
            options.ConfigureWarnings(warnings =>
                
            warnings.Ignore(CoreEventId.NavigationBaseIncludeIgnored));
        });

Or you can add similar things in the OnConfiguring method of Dbcontext或者你可以在 Dbcontext 的OnConfiguring方法中添加类似的东西

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
        .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.NavigationBaseIncludeIgnored, CoreEventId.NavigationBaseIncluded));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM