如何在ExecuteUpdate()之后获取更新后的记录?

huangapple go评论84阅读模式
英文:

How to get the updated record after ExecuteUpdate()?

问题

以下是翻译好的部分:

我正在为执行以下代码的方法进行集成测试(使用真实对象,没有模拟):

await _domainContext.User
                 .Where(x => x.Id == userId)
                 .ExecuteUpdateAsync(s =>
                     s.SetProperty(p => p.NumberOfFollowing, p => p.NumberOfFollowing + 1));

我可以在数据库中看到值已经正确增加(之前是0,更新后是1)。

之后,我想要断言 p.NumberOfFollowing 是否已经增加,所以我执行以下操作:

int updatedValue =  _domainContext.User.Single(x => x.Id == userId).NumberOfFollowing;

但是这个值总是零(没有获取到更新后的值)。

在测试中,我在设置中创建了数据库上下文,并且它与所有仓库共享。

英文:

I'm doing an integration test (real object, no mocks) for a method that executes the below code:

await _domainContext.User
             .Where(x => x.Id == userId)
             .ExecuteUpdateAsync(s =>
                 s.SetProperty(p => p.NumberOfFollowing, p => p.NumberOfFollowing + 1));

I can see on the DB that the value has been increased correctly (before was 0. After the update, it's 1)

After that, I want to assert p.NumberOfFollowing if it has been increased, so I do the below:

 int updatedValue =  _domainContext.User.Single(x => x.Id == userId).NumberOfFollowing;

But the value is always zero (not getting the updated one).

In the test I create the DB context in the Setup and it is shared with all repsitories

答案1

得分: 1

It seems that you are using the same instance of the context in both cases. And it seems that you have fetched the user somewhere during the request handling with enabled change tracking, which leads to the stale data. ExecuteUpdateAsync will bypass the change tracking mechanism, so you can try one of the following:

  1. Recreate the context: int updatedValue = GetNewContextSomeHow().User.Single(x => x.Id == userId).NumberOfFollowing;

  2. Clear change tracker:

_domainContext.ChangeTracker.Clear();
int updatedValue = _domainContext.User.Single(x => x.Id == userId).NumberOfFollowing;
  1. Explicitly reloading the data:
var user = _domainContext.User.Single(x => x.Id == userId); _domainContext.Entry(user).Reload();
int updatedValue = user.NumberOfFollowing;

Also maybe just int updatedValue = _domainContext.User.AsNoTracking().Single(x => x.Id == userId).NumberOfFollowing; will work but I can't test it at the moment.

英文:

It seems that you are using the same instance of the context in both cases. And it seems that you have fetched the user somewhere during the request handling with enabled change tracking, which leads to the stale data. ExecuteUpdateAsync will bypass the change tracking mechanism, so you can try one of the following:

  1. Recreate the context: int updatedValue = GetNewContextSomeHow().User.Single(x => x.Id == userId).NumberOfFollowing;

  2. Clear change tracker:

_domainContext.ChangeTracker.Clear();
int updatedValue = _domainContext.User.Single(x => x.Id == userId).NumberOfFollowing;
  1. Explicitly reloading the data:
var user = _domainContext.User.Single(x => x.Id == userId); _domainContext.Entry(user).Reload();
int updatedValue = user.NumberOfFollowing;

Also maybe just int updatedValue = _domainContext.User.AsNoTracking().Single(x => x.Id == userId).NumberOfFollowing; will work but I can't test it at the moment.

huangapple
  • 本文由 发表于 2023年4月13日 22:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定