简体   繁体   中英

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 . 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. 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.

Otherwise, you could device a SQL query and use ExecuteCommand on the entity object. LINQ queries are also an alternative to tackle batch executions.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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