英文:
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:
-
Recreate the context:
int updatedValue = GetNewContextSomeHow().User.Single(x => x.Id == userId).NumberOfFollowing;
-
Clear change tracker:
_domainContext.ChangeTracker.Clear();
int updatedValue = _domainContext.User.Single(x => x.Id == userId).NumberOfFollowing;
- 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:
-
Recreate the context:
int updatedValue = GetNewContextSomeHow().User.Single(x => x.Id == userId).NumberOfFollowing;
-
Clear change tracker:
_domainContext.ChangeTracker.Clear();
int updatedValue = _domainContext.User.Single(x => x.Id == userId).NumberOfFollowing;
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论