简体   繁体   English

Entity Framework Core 5 关闭加载 NavigationProperties

[英]Entity Framework Core 5 turn off loading NavigationProperties

How can I turn off loading navigation properties in EF Core 5 when I get data from the DBContext ?DBContext获取数据时,如何在 EF Core 5 中关闭加载导航属性?

I tried to add:我试图添加:

public DataContext(DbContextOptions<DataContext> options)
        : base(options)
{
   this.ChangeTracker.LazyLoadingEnabled = false;
}

but navigation properties are still loaded.但导航属性仍在加载。

Loaded NavProp加载的 NavProp

Edit : I added to AddDBContext option编辑:我添加到AddDBContext选项

UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

and it looks like this works.看起来这很有效。

Hope it doesn't spoil anything else?希望它不会破坏其他任何东西?

Edit-2 UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); Edit-2 UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); - ok its not what I need.... - 好吧,这不是我需要的......

I getting data like:我得到如下数据:

List<Data> dataList = await _dataContext.Data.Where(w => idMeters.Contains(w.IdMeter.Value)).ToListAsync();

In result I have Data object with IdMeterNavigation, and IdMeterNavigation have another NavigationProperties... I don't want to load any NavigationProperties how I can do that?结果我有带有 IdMeterNavigation 的数据 object,而 IdMeterNavigation 有另一个 NavigationProperties...我不想加载任何 NavigationProperties 我该怎么做?

I even removed virtual from class fields and they are still loaded.我什至从 class 字段中删除了virtual并且它们仍然被加载。

Default behaviour of EF is to not load related entities. EF 的默认行为是不加载相关实体。 In order to load related entities you could use:为了加载相关实体,您可以使用:

  • eager loading急切加载
  • explicit loading显式加载
  • select loading select加载
  • lazy loading延迟加载

So, for me it seems that you have ENABLED lazy loading or you actually do a SELECT loading using "Select" LINQ extension or kind of automapper software.所以,对我来说,您似乎启用了延迟加载,或者您实际上使用“选择”LINQ 扩展或某种自动映射器软件进行了SELECT加载。

Check if you havent enabled lazy loading for sure... https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy检查您是否确定没有启用延迟加载... https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy

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

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