gpt4 book ai didi

c# - Entity Framework 中的 SaveChanges 与 AcceptAllChanges

转载 作者:IT王子 更新时间:2023-10-29 04:49:10 27 4
gpt4 key购买 nike

_context.SaveChanges_context.AcceptAllChanges() 之间的区别是 AcceptAllChanges() 是一种从数据库或回滚(丢弃)用户在未使用 SaveChanges()

时所做的更改

最佳答案

ObjectContext.AcceptAllChanges Method - MSDN

If the SaveChanges method was called and the AcceptAllChangesAfterSave was not specified, the user must call the AcceptAllChanges method. The AcceptAllChanges method is useful in the scenario where a transaction has failed and a user wants to retry.

您可能会看到:http://blogs.msdn.com/b/alexj/archive/2009/01/11/savechanges-false.aspx

If you call SaveChanges() or SaveChanges(true),the EF simply assumes that if its work completes okay, everything is okay, so it will discard the changes it has been tracking, and wait for new changes.

Unfortunately though if something goes wrong somewhere else in the transaction, because the EF discarded the changes it was tracking, we can’t recover.

This is where SaveChanges(false) and AcceptAllChanges() come in.

SaveChanges(false) tells the EF to execute the necessary database commands, but hold on to the changes, so they can be replayed if necessary.

Now if the broader transaction fails you can retry the EF specific bits, with another call to SaveChanges(false). Alternatively you can walk through the state-manager to log what failed.

Once the broader transaction succeeds, you simply call AcceptAllChanges() manually, and the changes that were being tracked are discarded.

关于c# - Entity Framework 中的 SaveChanges 与 AcceptAllChanges,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859304/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com