Error Using ADO.NET Entity Framework

前端 未结 2 1984
北恋
北恋 2021-01-19 13:21

I want to convert a list to EntityCollection.

List x = methodcall();
EntityCOllection y = new EntityCollection();

foreach(T t in          


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 14:01

    It sounds like x is the result of an ObjectContext query. Each ObjectContext tracks the entities it reads from the database to enable update scenarios. It tracks the entities to know when (or if) they are modified, and which properties are modified.

    The terminology is that the entities are attached to the ObjectContext. In your case, the entities in x are still attached to the ObjectContext that materialized them, so you can't add them to another EntityCollection at the same time.

    You may be able to do that if you first Detach them, but if you do that, the first ObjectContext stops tracking them. If you never want to update those items again, it's not a problem, but if you later need to update them, you will have to Attach them again.

提交回复
热议问题