1

为什么下面返回的实体不更新?即使 stocktakeid 变量具有值,它们在 Get 上为空,并且在 Save(在数据库中)之后保持为空。

protected void GetPipelineState()
        {
            InventoryModelDataContext context = new InventoryModelDataContext(this.ConnectionString);
            var f = from pe in context.StockTakePipelines
                    where pe.StockTakeId == null
                    select pe;
            this.PipeLine = f;
        }

        protected void SavePipelineState()
        {
            InventoryModelDataContext context = new InventoryModelDataContext(this.ConnectionString);
            foreach (StockTakePipeline p in this.PipeLine)
            {
                p.StockTakeId = this.StockTakeId;
            }
            context.SubmitChanges();
        }

编辑:重新PK

在此处输入图像描述

4

1 回答 1

2

您将实体从context本地更改为GetPipelineState(),然后调用本地SubmitChanges()到。contextSavePipelineState()

尝试更多类似的东西:

    protected IQueryable<StockTakePipeline> GetPipelineState(InventoryModelDataContext context)
    {
        return from pe in context.StockTakePipelines
                where pe.StockTakeId == null
                select pe;
    }

    protected void SavePipelineState()
    {
        using(InventoryModelDataContext context = new InventoryModelDataContext(this.ConnectionString));
        {
          foreach (StockTakePipeline p in GetPipelineState(context))
          {
              p.StockTakeId = this.StockTakeId;
          }
          context.SubmitChanges();
        }
    }

编辑:

只是给其他人发现这个问题的说明。导致实体不更新的另一件事是,如果他们不实现INotifyPropertyChangingand INotifyPropertyChanged,他们通常会这样做,但如果您手动编写类并忘记实现这些,或者表没有主键 (在这种情况下,无论如何都无法对给定的行进行更新,因为它无法被识别,并且代码生成器放弃了那些现在毫无意义的接口的实现作为优化)。

于 2012-08-15T10:24:24.423 回答