在实体框架查询期间修改数据库表

时间:2020-12-28 04:32:35

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?
  • 在执行此操作时,我可以在SSMS中向dbo.MyObject添加新行吗?如果是这样,他们是否在循环中被选中?

  • Can I delete rows in dbo.MyObject in SSMS while this is executing? Will the loop still try to obtain them?
  • 在执行此操作时,我可以删除SSMS中dbo.MyObject中的行吗?循环仍然会尝试获取它们吗?

  • What level of the EF/ADO.NET stack determines the answer to this question?
  • 什么级别的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 的工作原理,特别是EF / ADO.NET。

#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 的工作原理,特别是EF / ADO.NET。