检查是否有任何待保存的更改要保存 [英] Check if there are any pending changes to be saved

查看:26
本文介绍了检查是否有任何待保存的更改要保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在实体框架中找出我的实体上下文中是否有未保存的更改?

Is there a way to find out whether there are unsaved changes in my entity context, in the Entity Framework?

推荐答案

这可能有效(如果您所说的更改是指添加、删除和修改的实体):

This might work (if by changes you mean added, removed and modified entities):

bool changesMade = (context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Count() +
                    context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).Count() +
                    context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified).Count()
                    ) > 0;

改进的代码:

bool changesMade = context.
                   ObjectStateManager.
                   GetObjectStateEntries(EntityState.Added | 
                                         EntityState.Deleted | 
                                         EntityState.Modified
                                        ).Any();

这篇关于检查是否有任何待保存的更改要保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆