当前位置: 首页 > news >正文

Tracking vs. No-Tracking Queries

学习链接

Tracking queries

By default, queries that return entity types are tracking. A tracking query means any changes to entity instances are persisted by SaveChanges.

var blog = context.Blogs.SingleOrDefault(b => b.BlogId == 1);
blog.Rating = 5;
context.SaveChanges();

Keyless entity types are never tracked. Wherever this article mentions entity types, it refers to entity types which have a key defined.

No-tracking queries

No-tracking queries are useful when the results are used in a read-only scenario. They’re generally quicker to execute because there’s no need to set up the change tracking information. If the entities retrieved from the database don’t need to be updated, then a no-tracking query should be used. An individual query can be set to be no-tracking. A no-tracking query also give results based on what’s in the database disregarding any local changes or added entities.

var blogs = context.Blogs.AsNoTracking().ToList();context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var blogs = context.Blogs.ToList();

Configuring the default tracking behavior

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFQuerying.Tracking;Trusted_Connection=True").UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}

Even if the result type of the query isn’t an entity type, EF Core will still track entity types contained in the result by default. In the following query, which returns an anonymous type, the instances of Blog in the result set will be tracked.

var blog = context.Blogs.Select(b =>new { Blog = b, PostCount = b.Posts.Count() });

You can configure a navigation in the model to be included every time the entity is loaded from the database using AutoInclude method. It has same effect as specifying Include with the navigation in every query where the entity type is returned in the results. Following example shows how to configure a navigation to be automatically included.

modelBuilder.Entity<Theme>().Navigation(e => e.ColorScheme).AutoInclude();

If for a particular query you don’t want to load the related data through a navigation, which is configured at model level to be auto-included, you can use IgnoreAutoIncludes method in your query. Using this method will stop loading all the navigations configured as auto-include by the user. Running a query like below will bring back all themes from database but won’t load ColorScheme even though it’s configured as auto-included navigation.

using (var context = new BloggingContext())
{var themes = context.Themes.IgnoreAutoIncludes().ToList();
}
http://www.lryc.cn/news/229266.html

相关文章:

  • Centos7安装frps实现内网穿透
  • cryptopp Base64Encoder \n问题
  • 一种艺术风格的神经算法:总结与实现
  • 【Mysql系列】Mysql基础篇
  • C++面试题之C++中的指针参数传递和引用参数传递
  • [Android]Unresolved reference: appcompat
  • 网络运维Day14
  • Mac常用软件安装
  • node 文件上传操作(前端 form表单上传 formData上传 后端 node 使用express+multer)
  • 容器数据卷+MYSQL实战
  • 开发者测试2023省赛--UnrolledLinkedList测试用例
  • HoudahGeo 6 for Mac:掌控地理位置信息的强大工具
  • Xilinx Artix7-100T低端FPGA解码MIPI视频,基于MIPI CSI-2 RX Subsystem架构实现,提供工程源码和技术支持
  • C与汇编深入分析
  • MySQL中外键的使用及外键约束策略
  • Home Assistant使用ios主题更换背景
  • 深入了解鼠标光标的设置过程
  • 数据结构-散列表
  • 一款IT团队都在用的私有化知识库,技术开放,还开源了!
  • 解决 docker compose 官方 MySQL 镜像在容器中不能输入中文的问题
  • 基于连续Hopfield神经网络优化——旅行商问题优化计算
  • SpringBoot整合Activiti7——定时器事件(九)
  • 轻量封装WebGPU渲染系统示例<29>- 深度模糊DepthBlur(源码)
  • LeetCode226. Invert Binary Tree
  • Java设计模式-创建型模式-建造者模式
  • PyQt中QFrame窗口中的组件不显示的原因
  • git 命令行回退版本
  • IntelliJ IDEA 安装 GitHub Copilot插件 (最新)
  • viewpage选择器
  • vue中如何将json数组指定的key赋值给el-form-item并均匀的分成2列