Let's say I have an Entity Framework context context
, and execute the following:
假设我有一个Entity Framework上下文,并执行以下操作:
foreach (var item in context.Set<MyObject>())
DoSomethingThatTakesTenSeconds();
- Can I add new rows to
dbo.MyObject
in SSMS while this is executing? If so, are they picked up in the loop? - Can I delete rows in
dbo.MyObject
in SSMS while this is executing? Will the loop still try to obtain them? - What level of the EF/ADO.NET stack determines the answer to this question?
在执行此操作时,我可以在SSMS中向dbo.MyObject添加新行吗?如果是这样,他们是否在循环中被选中?
在执行此操作时,我可以删除SSMS中dbo.MyObject中的行吗?循环仍然会尝试获取它们吗?
什么级别的EF / ADO.NET堆栈决定了这个问题的答案?
1 个解决方案
#1
1
When you access the first item in IQueryable<T>
, the query is executed against database and results are materialized into collection of objects. Any modification of database will not change that collection. Answers to your questions:
当您访问IQueryable
1) You can add new objects, they are not picked up.
1)您可以添加新对象,但不会拾取它们。
2) You can delete objects, they will be in loop.
2)您可以删除对象,它们将处于循环中。
3) It is how IQueryable<T>
works, not EF/ADO.NET in particular.
3)IQueryable
#1
1
When you access the first item in IQueryable<T>
, the query is executed against database and results are materialized into collection of objects. Any modification of database will not change that collection. Answers to your questions:
当您访问IQueryable
1) You can add new objects, they are not picked up.
1)您可以添加新对象,但不会拾取它们。
2) You can delete objects, they will be in loop.
2)您可以删除对象,它们将处于循环中。
3) It is how IQueryable<T>
works, not EF/ADO.NET in particular.
3)IQueryable