ef之 entitystate状态
2015-10-08 22:19
351 查看
之前使用EF,我们都是通过调用SaveChanges方法把增加/修改/删除的数据提交到数据库,但是上下文是如何知道实体对象是增加、修改还是删除呢?答案是通过EntityState枚举来判断的,我们看一个方法:
注:使用EntityState需添加引用system.data
跑下程序,输出结果为:Unchanged。从英文意思我们已经猜到一二:取出来的数据是Unchanged,那么添加、修改、删除自然也就是Added、Modified、Deleted了。我们在EntityState上按F12定位到其定义看看:
的确,当调用SaveChanges方法的时候,EF会根据EntityState这个枚举检测到实体的状态,然后执行相应的增/删/改操作。它们的具体意思分别为:
An entity can be in one of five states as defined by the EntityState enumeration. These states are:
Added: the entity is being tracked by the context but does not yet exist in the database
Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database
Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified
Deleted: the entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called
Detached: the entity is not being tracked by the context【database may be not existed】
SaveChanges does different things for entities in different states:
Unchanged entities are not touched by SaveChanges. Updates are not sent to the database for entities in the Unchanged state.
Added entities are inserted into the database and then become
Unchanged when SaveChanges returns.
Modified entities are updated in the database and then become
Unchanged when SaveChanges returns.
Deleted entities are deleted from the database and are then detached from the context.
/// <summary> /// 查看实体状态 /// </summary> private static void GetOneEntityToSeeEntityState() { using (var context = new DbContexts.DataAccess.BreakAwayContext()) { var destination = context.Destinations.Find(4); EntityState stateBefore = context.Entry(destination).State; Console.WriteLine(stateBefore); } }
注:使用EntityState需添加引用system.data
跑下程序,输出结果为:Unchanged。从英文意思我们已经猜到一二:取出来的数据是Unchanged,那么添加、修改、删除自然也就是Added、Modified、Deleted了。我们在EntityState上按F12定位到其定义看看:
的确,当调用SaveChanges方法的时候,EF会根据EntityState这个枚举检测到实体的状态,然后执行相应的增/删/改操作。它们的具体意思分别为:
An entity can be in one of five states as defined by the EntityState enumeration. These states are:
Added: the entity is being tracked by the context but does not yet exist in the database
Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database
Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified
Deleted: the entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called
Detached: the entity is not being tracked by the context【database may be not existed】
SaveChanges does different things for entities in different states:
Unchanged entities are not touched by SaveChanges. Updates are not sent to the database for entities in the Unchanged state.
Added entities are inserted into the database and then become
Unchanged when SaveChanges returns.
Modified entities are updated in the database and then become
Unchanged when SaveChanges returns.
Deleted entities are deleted from the database and are then detached from the context.
相关文章推荐
- 图像处理函数详解——imfilter
- 电脑史话(2)
- 51nod 1004 n^n的末位数字
- 区分Activity的四种加载模式 (写的真心棒)
- 四种会话跟踪技术
- 电脑史话(3)
- CodeForces 510E Fox And Dinner
- 《剑指Offer》面试题:按字典序打印出该字符串中字符的所有排列
- Android Studio下的library转换成Eclipse下可使用的library
- 编程是一个及其痛苦的事
- 组织Go的源码
- 使用 SQLiteDatabase 操作 SQLite 数据库
- 关于PADS的一些概念和实用技巧(一)
- codeforces - 578B - "Or" Game
- BestCoder Round #49 HDU5339 Untitled
- WXHL 学习文章连载十三
- 毕业一年,回顾过去,再次起航!
- 电脑史话(4)
- C#与数据结构
- 一个简单的HTTP服务器(多线程)