[英]Entity Framework Change Tracking (Read Only) query
Just wanted peoples opinions on a scenario: 只是想让人们对一个场景发表意见:
Would it be more efficient to: 这样做会更有效吗?
Or is this trivial? 还是这很琐碎?
My scenario is that we have a health check routine that does a select on a table every 10 seconds and very rarely needs to be updated (Only updates the record if a new version has been deployed). 我的情况是,我们有一个运行状况检查例程,该例程每10秒对一个表进行一次选择,并且很少需要更新(仅在部署了新版本时才更新记录)。 So should we do the health check with change tracking turned off turned on?
那么我们应该在关闭变更跟踪功能的情况下进行健康检查吗?
According to your use case, I think No-tracking
queries will give big performance boost
to your app. 根据您的用例,我认为
No-tracking
查询将big performance boost
您的应用程序的big performance boost
。
So you can do that using AsNoTracking()
因此,您可以使用
AsNoTracking()
using (var context = new HelthContext())
{
var patients = context.Patients.AsNoTracking().ToList();
}
If you have an entity
that you know already exists
in the database but which is not currently being tracked
by the context
then you can tell the context
to track the entity using the Attach
method on DbSet
as shown below. 如果您知道数据库中
already exists
一个entity
,但是context
not currently being tracked
,则可以使用DbSet
上的Attach
方法告诉context
跟踪该实体,如下所示。
var existingPatient = new Patient { Id = 1, Name = "Patient 1" };
using (var context = new HelthContext())
{
context.Patients.Attach(existingPatient );
// Do some more work...
context.SaveChanges();
}
Reference : Entity states and SaveChanges 参考: 实体状态和SaveChanges
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.