Error updating entity

Posts   
 
    
Posts: 18
Joined: 21-Mar-2011
# Posted on: 01-Jun-2011 07:07:30   

Hello, i am using LLBL Gen v2.6, and while updating entity i get error -500500, message - During a save action an entity's update action failed. The entity which failed is enclosed.Type:SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyExceptionStackTrace: at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions, Int32&totalAmountSaved)at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse)at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, Boolean recurse)at CFR.Services.CFRService.UpdateClaimedInvoice(CfrcClaimedInvoiceEntity CInvoice, Boolean&update) in D:\TMIMT\TmProjects\cfrCoupons\CFRCouponManagementStudio\Source\ApplicationServer\CFR.Services\CFRService.cs:line 5248

Please, advice me on it. Thank you.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Jun-2011 11:30:55   

Please post a simple code snippet for what you were trying.

Posts: 18
Joined: 21-Mar-2011
# Posted on: 01-Jun-2011 12:08:38   

Here is my method for update: public CfrcClaimedInvoiceEntity UpdateClaimedInvoice(CfrcClaimedInvoiceEntity CInvoice, ref bool update) { update = false; try { using (DataAccessAdapter adapter = new DataAccessAdapter()) {

                foreach (var i in CInvoice.CfrcCouponEntryDetail)
                {
                    if (i.CouponMisredemptionId==-1)
                    {
                        i.CouponMisredemptionId = null;
                    }
                }                   
                CInvoice.IsNew = false;
                adapter.SaveEntity(CInvoice, true, false); //it will update coupon entry too
                foreach (var i in CInvoice.CfrcClaimedInvoiceDetail)
                {
                    i.ShipmentId = CInvoice.ShipmentId;
                   // i.LotId = CInvoice.LotId;
                    adapter.SaveEntity(i,true,false);
                }

                //update coupons
                foreach (var i in CInvoice.CfrcCouponEntryDetail)
                {
                    i.ShipmentId = CInvoice.ShipmentId;
                   // i.LotId = CInvoice.LotId;
                    adapter.SaveEntity(i, true);
                }
                //delete removed claimed
                if (CInvoice.CfrcClaimedInvoiceDetail.RemovedEntitiesTracker.Count != 0)
                {
                    adapter.DeleteEntityCollection(CInvoice.CfrcClaimedInvoiceDetail.RemovedEntitiesTracker);
                }
                //delete removed coupons
                if (CInvoice.CfrcCouponEntryDetail.RemovedEntitiesTracker.Count != 0)
                {
                    adapter.DeleteEntityCollection(CInvoice.CfrcCouponEntryDetail.RemovedEntitiesTracker);
                }
                //delete removed ShipmentActions
                if (CInvoice.CfrcShipmentAction.RemovedEntitiesTracker.Count != 0)
                {
                    adapter.DeleteEntityCollection(CInvoice.CfrcShipmentAction.RemovedEntitiesTracker);
                }

                foreach (var i in CInvoice.CfrcShipmentAction)
                {
                    if (!i.IsNew)
                    {
                        adapter.SaveEntity(i, true);
                    }
                }

                if (CInvoice.IsNeedMisredemptionControl)
                {
                    AddTask("C_CI", CInvoice.CfrcShipmentAction[CInvoice.CfrcShipmentAction.Count - 1].UserId, CInvoice.ClaimedInvoiceId);
                }
                else
                {
                    ProcessTask("C_CI", CInvoice.ClaimedInvoiceId);
                }
                if (CInvoice.CfrcShipmentAction.Count == 2) //Waiting for Approve
                {
                    AddTask("A_CI", CInvoice.CfrcShipmentAction[CInvoice.CfrcShipmentAction.Count - 1].UserId, CInvoice.ClaimedInvoiceId);
                }
                else
                {
                    ProcessTask("A_CI", CInvoice.ClaimedInvoiceId);
                }


                update = true;
            }
        }
        catch (Exception e)
        {
            Exception sqlEx = e.GetBaseException();
            int errorNum = 0;
            if (sqlEx != null)
            {
                if (sqlEx is System.Data.SqlClient.SqlException)
                    errorNum = (sqlEx as System.Data.SqlClient.SqlException).Number;
                if (sqlEx is SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyException)
                    errorNum = -500500;
            }
            throw new FaultException<WCFServiceFault>(
                new WCFServiceFault() { StackTrace = e.StackTrace, FaultMessage = e.Message +"Type:"+ e.GetType()+"StackTrace:"+ e.StackTrace, SqlErrorNumber = errorNum });
        }

        return CInvoice;
    }

And here is my method from tests class: public bool UpdateClaimedInvoice(CfrcClaimedInvoiceEntity cI,int ciId, int shipmentId) { /<FaultMessage>During a save action an entity's update action failed. The entity which failed is enclosed.Type:SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyExceptionStackTrace: at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions, Int32&totalAmountSaved)at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse)at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, Boolean recurse)at CFR.Services.CFRService.UpdateClaimedInvoice(CfrcClaimedInvoiceEntity CInvoice, Boolean&update) in D:\work\cfrcoupons\CFRCouponManagementStudio\Source\ApplicationServer\CFR.Services\CFRService.cs:line 4813</FaultMessage>/

        if(cI==null)
            cI = GetClaimedInvoice(shipmentId, ciId);

        bool updated = false;
        cI.Fields.State = EntityState.Fetched;
        cI.PostageValue = Helper.GetRadomDecimal(5,30);

        try
        {
            cI = cfrService.UpdateClaimedInvoice(cI, ref updated);
        }
        catch (Exception e)
        {
            Exception sqlEx = e.GetBaseException();
            int errorNum = 0;
            if (sqlEx != null)
            {
                if (sqlEx is System.Data.SqlClient.SqlException)
                    errorNum = (sqlEx as System.Data.SqlClient.SqlException).Number;
                if (sqlEx is SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyException)
                    errorNum = -500500;
            }
            throw new FaultException<WCFServiceFault>(
                new WCFServiceFault() { StackTrace = e.StackTrace, FaultMessage = e.Message + "Type:" + e.GetType() + "StackTrace:" + e.StackTrace, SqlErrorNumber = errorNum });
        }

        return updated;
    }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Jun-2011 12:28:31   

Could you please cut it down to the minimum lines to reproduce the issue. Try to reproduce the issue with 4 or 5 lines of code.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 01-Jun-2011 15:02:03   

This happens when the # of rows affected by an update statement is 0. This often occurs when an entity for update is actually already removed:

UPDATE Customers SET CompanyName = @p WHERE CustomerId = @p2

this doesn't do anything if the customerid specified in @p2 isn't in the customers table anymore, and therefore returns 0 rows affected, which results in a concurrencyexception.

You also get this if rowcounting is disabled on the database server, which is something you can configure in sqlserver's server parameters (rowcounting enable/disable on connection). If you occasionally get this, it's not the rowcounting option but the missing entity.

Frans Bouma | Lead developer LLBLGen Pro