简体   繁体   English

我如何批量更新实体框架?

[英]how can i bulk update entity framework?

Hi all i using Entity Framework generated by code smith tools i have a problem with bulk update . 大家好,我使用代码编辑器工具生成的Entity Framework,但批量更新存在问题。 Because i have many row in the grid and i want to update record using bulk update . 因为我在网格中有很多行,所以我想使用批量更新来更新记录。 I have tried to do that but not success my code is below 我试图这样做,但没有成功,我的代码如下

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*/)
    {


    }

I think that you are creating the PriceSystemItems object and then sending it for update. 我认为您正在创建PriceSystemItems对象,然后将其发送以进行更新。 This will fail. 这将失败。 There is a limitation in Entity Framework which says that you need to retrieve the object to the memory before you modify it. 实体框架中有一个限制,即您需要在修改对象之前将对象检索到内存中。 Either you will have to retrieve the priceSystemItem, modify it and push it to the collection and finally use SubmitChanges on the entity object. 您要么必须检索priceSystemItem,然后对其进行修改并将其推送到集合中,然后最后对实体对象使用SubmitChanges。

Otherwise, you could device a SQL query and use ExecuteCommand on the entity object. 否则,您可以使用SQL查询并在实体对象上使用ExecuteCommand。 LINQ queries are also an alternative to tackle batch executions. LINQ查询还是解决批处理问题的替代方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM