0

大家好,我使用由 code smith 工具生成的实体框架,我遇到了批量更新的问题。因为我在网格中有很多行,我想使用批量更新来更新记录。我试图这样做但没有成功我的代码如下

try
    {
        TList<PriceSystemItems> _priceSystemCollection = new TList<PriceSystemItems>() ;


        for (int i = 0; i < gvService.Rows.Count; i++)
        {
            _priceSystemItems = new PriceSystemItems();

            EntityDropDownList dataServiceTypeId = (EntityDropDownList)gvService.Rows[i].FindControl("dataServiceTypeId1");
            EntityDropDownList dataPricePlanId = (EntityDropDownList)gvService.Rows[i].FindControl("dataPricePlanId1");
            EntityDropDownList dataNoMatchPlanId = (EntityDropDownList)gvService.Rows[i].FindControl("dataNoMatchPlanId1");
            EntityDropDownList dataSurchargePlanId = (EntityDropDownList)gvService.Rows[i].FindControl("dataSurchargePlanId1");
            CheckBox chkDefault = (CheckBox)gvService.Rows[i].FindControl("chkDefault");
            Label lblPricePlanId = (Label)gvService.Rows[i].FindControl("lblPricePlanId");

            _priceSystemItems.ServiceTypeId = int.Parse(dataServiceTypeId.SelectedValue);
            _priceSystemItems.PriceSystemId = _priceSystemId;
            _priceSystemItems.NoMatchAltPlanId = int.Parse(dataNoMatchPlanId.SelectedValue);
            _priceSystemItems.SurchargePlanId = int.Parse(dataSurchargePlanId.SelectedValue);
            _priceSystemItems.IsDefault = chkDefault.Checked;
            _priceSystemItems.PricePlanId = int.Parse(lblPricePlanId.Text);
            _priceSystemItems.OriginalPriceSystemId = _priceSystemId;
            _priceSystemItems.OriginalServiceTypeId = int.Parse(dataServiceTypeId.SelectedValue);

           _priceSystemCollection.Add(_priceSystemItems);


        }

         _priceSystemItemsService.Update(_priceSystemCollection);
    }
    catch (Exception /*ex*/)
    {


    }
4

1 回答 1

0

我认为您正在创建 PriceSystemItems 对象,然后将其发送以进行更新。这将失败。Entity Framework 中有一个限制,即您需要在修改对象之前将其检索到内存中。要么您必须检索 priceSystemItem,对其进行修改并将其推送到集合中,最后在实体对象上使用 SubmitChanges。

否则,您可以设置 SQL 查询并在实体对象上使用 ExecuteCommand。LINQ 查询也是处理批处理执行的替代方法。

请查看http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx

于 2012-12-18T06:04:30.823 回答