【问题标题】:Entity Framework 2.0: Cannot update EntityCollectionEntity Framework 2.0:无法更新 EntityCollection
【发布时间】:2010-04-16 14:19:05
【问题描述】:

假设我们有“EntityCollection 产品”。

那么下面的就不行了:

foreach (var product in products)
{
 product.LastUpdate = DateTime.Now();
}

由于您也无法索引 EntityCollection,您如何修改 en EntityCollection 中的实体?

【问题讨论】:

  • 您能否详细说明“不起作用”?
  • 好吧,当执行 Visual Studio 时抱怨“产品”是一个可枚举的集合,在枚举过程中你不能改变它。然后,当我使用 "for (int i = 0; i
  • 你能显示产品声明/初始化的代码吗?

标签: c# entity-framework


【解决方案1】:

Try this,我创建了一个通用方法来更新 EntityCollection。给我你的反馈。

您只需在您的产品实体集合中修改您的产品。打电话给我的GenericUpdateEntityCollection(YourEntityCollection, YourObjectContext); 就可以了

【讨论】:

    【解决方案2】:

    你可以试试这个吗?

    foreach (var product in products.ToArray())
    {
         product.LastUpdate = DateTime.Now();
    }
    

    您必须复制元素以使用ToArray() 进行枚举,因此您可以更改products

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 2016-05-16
      • 2021-04-21
      • 2022-12-07
      • 1970-01-01
      • 2020-02-13
      • 2020-08-05
      相关资源
      最近更新 更多