Is query to new added object possible in MS Entity Framework

北战南征 提交于 2019-12-07 04:46:45

问题


Is there a way to query or just access newly added object (using ObjectContext.AddObject method) in Entity Framework? I mean situation when it is not yet saved to data store using SaveChanges

I understand that queries are translated to underlying SQL and executed against data store, and it don't have this new object yet. But anyway, I'm curious - if it is not oficially supported, maybe it is possible in theory. If it's not, how developer can deal with it? Manually track new objects and query them using Linq to objects?

The same question also applies to LinqToSql.


回答1:


In EF, if you use this code, you have all the entities that are already loaded in the context (including newly added ones) :

context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged).Select(o => o.Entity).OfType<YourObjectType>()



回答2:


"The same question also applies to LinqToSql."

For LINQ-to-SQL, look at DataContext.GetChangeSet(); this has 3 separate collections for the pending .Inserts, .Updates and .Deletes

Note that the ChangeSet is a snapshot of when the GetChangeSet() method is called; you need to re-query to see any additional changes.



来源:https://stackoverflow.com/questions/326186/is-query-to-new-added-object-possible-in-ms-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!