实体框架 4.0 中的回滚

发布于 2024-09-08 22:16:18 字数 667 浏览 4 评论 0原文

我有一个客户类,其中包括一个联系人类,该类存储该特定客户的所有联系人,因此它们与外键关系链接。

这是我的场景:用户编辑客户的信息(包括联系人),然后决定点击“取消”按钮。联系人绑定到网格,因此每次进行编辑/添加/删除时,它都会自动更新缓存数据库上下文中的联系人实体。那么我如何回滚用户对联系人实体所做的所有更改?

我尝试了以下方法(在谷歌搜索答案后):

    public static void CustomerRollback(Customer customer)
    {
        dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, customer);
        dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, customer.Contacts);
    }

但这不起作用。还有其他想法吗?请注意,我的问题仅在于回滚联系人实体。我想首先导致问题的原因是网格自动更新缓存的实体。因此,当我尝试取消时,每个联系人的 EntityState 已更改为修改状态(EntityState.Added、EntityState.Deleted 等)。我是否需要循环访问联系人并检查其 EntityState 属性并对其执行某些操作?

谢谢 凯撒

i have a Customer class which includes a Contact class that stores all the contacts for that particular customer, so they are linked with a foreign key relationship.

Here is my scenario: the user Edits the customer's info including the contacts, and then decides to hit the "Cancel" button. The contacts are bound to a grid so everytime an edit/add/delete is made it automatically updates the Contact entity in the cached database context. So how can i rollback all the changes made by the user to the Contact entity?

I tried the following (after searching google for answers):

    public static void CustomerRollback(Customer customer)
    {
        dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, customer);
        dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, customer.Contacts);
    }

But that didn't work. Any other ideas? Please note that my problem is only with rolling back the Contacts entity. I guess what's causing the issue in the first place is that the grid automatically updates the cached entities. So when i try to cancel, the EntityState for each contact has already changed to a modified state (EntityState.Added, EntityState.Deleted, etc.). Do i need to loop through the contacts and check their EntityState property and do something with it?

Thanks
Caesar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2024-09-15 22:16:18

这就是我所做的,这是 Entity Framework 1.0 的一个解决方法,我希望 EF 4.0 有一种更简单的方法来回滚对象上下文。如果您知道更好的方法,请与我分享,目前看来这可行:

        // delete added objects that did not get saved
        foreach (var entry in dbContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
        {
            if (entry.Entity != null)
                dbContext.DeleteObject(entry.Entity);
        }
        // Refetch modified objects from database
        foreach (var entry in dbContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
        {
            if (entry.Entity != null)
                dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, entry.Entity);
        }
        // Recover modified objects that got deleted
        foreach (var entry in dbContext.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
        {
            if (entry.Entity != null)
                dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, entry.Entity);
        }

        dbContext.AcceptAllChanges();

So here is what i did, which is a workaround trick from Entity Framework 1.0, i was hoping that EF 4.0 has a much simpler way to rollback the object context. Please share with me if you know of a better way, this seems to work for the time being:

        // delete added objects that did not get saved
        foreach (var entry in dbContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
        {
            if (entry.Entity != null)
                dbContext.DeleteObject(entry.Entity);
        }
        // Refetch modified objects from database
        foreach (var entry in dbContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
        {
            if (entry.Entity != null)
                dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, entry.Entity);
        }
        // Recover modified objects that got deleted
        foreach (var entry in dbContext.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
        {
            if (entry.Entity != null)
                dbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, entry.Entity);
        }

        dbContext.AcceptAllChanges();
救赎№ 2024-09-15 22:16:18

查看 AcceptAllChanges 方法。 SaveChanges(SaveOptions) 也很有帮助。
另一个解决方案是使用 ObjectContext 实例作为工作单元并为操作创建它,并且仅在必要时(当用户确认有必要时)保存更​​改,只需在另一种情况下处理上下文 - 这将丢弃更改根据上下文进行。

Take a look at the AcceptAllChanges method. SaveChanges(SaveOptions) can also be helpful.
One more solution is to use the ObjectContext instance as Unit Of Work and to create it for an operation, and to save changes only when necessary (when user confirms that it is necessary) just disposing the context in another case - this will discard the changes made to the context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文