ORMConcurrencyException

Posts   
 
    
Luca75
User
Posts: 37
Joined: 10-Feb-2012
# Posted on: 13-May-2016 18:18:08   

Hello I have a problem when I save twice the same entity created by code. I set the flag is New = false because I want to get an update. The DQE creates the update and runs it but pui lifts me except a competition .. but it should not do so because the sql is correct I will have only zero rows updated but does not mean that there was a problem of competition .. How can I avoid the exception? In any case I would like to distinguish it from a real exception of competition that may occur .. For me zero rows updated it means that the contents of the collection was exactly the same as the database..se I run the update from the MySql console will not get an error but simply zero rows updated .. I do not know if it is a problem of version 5 with 3 I have never encountered this problem .. Thank you


Query form script SQL
UPDATE ul01s_customers SET CustomerID = 148, ContactName = 'ddrr', ContactTitle = '1', CompanyName = '1', Country = '1' WHERE CustomerID = 148;
Result:
SQL.sql: 0 rows updated [0,022s]


Same query From LLBLGen5

Method Enter: CreateSingleTargetUpdateDQ(4)
Generated Sql query: 
    Query: UPDATE `ul01s_customers` SET `CompanyName`=@p1, `ContactName`=@p2, `ContactTitle`=@p3, `Country`=@p4 WHERE ( `ul01s_customers`.`CustomerID` = @p5)
    Parameter: @p1 : AnsiString. Length: 255. Precision: 0. Scale: 0. Direction: Input. Value: "1".
    Parameter: @p2 : AnsiString. Length: 255. Precision: 0. Scale: 0. Direction: Input. Value: "ddrr".
    Parameter: @p3 : AnsiString. Length: 255. Precision: 0. Scale: 0. Direction: Input. Value: "1".
    Parameter: @p4 : AnsiString. Length: 255. Precision: 0. Scale: 0. Direction: Input. Value: "1".
    Parameter: @p5 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 148.

Method Exit: CreateSingleTargetUpdateDQ(4)
Method Exit: CreateUpdateDQ(4)
Method Enter: DataAccessAdapterBase.ExecuteActionQuery
Method Enter: DataAccessAdapterBase.OpenConnection
Method Exit: DataAccessAdapterBase.OpenConnection
Method Exit: DataAccessAdapterBase.ExecuteActionQuery
Method Enter: DataAccessAdapterBase.Rollback
Method Exit: DataAccessAdapterBase.Rollback
Eccezione generata: 'SD.LLBLGen.Pro.ORMSupportClasses.ORMConcurrencyException' in SD.LLBLGen.Pro.ORMSupportClasses.dll
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131076282482120735): caricamento di 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll' completato. Caricamento dei simboli ignorato. Il modulo è ottimizzato e l'opzione del debugger 'Solo codice utente' è abilitata.


daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-May-2016 13:47:29   

What is the .net code that generates that sql?

David Elizondo | LLBLGen Support Team
Luca75
User
Posts: 37
Joined: 10-Feb-2012
# Posted on: 16-May-2016 19:21:37   

 MyEntity =new Ul01sCustomerEntity();
                MyEntity.CustomerId = 148;
                MyEntity.CompanyName = "ddrr";
                MyEntity.ContactName =1;
                MyEntity.ContactTitle = 1;
                MyEntity.Country = 1;

                Ul01sCustomerEntityCollection.Add(MyEntity);
            

            using (var adapter = new DataAccessAdapter())
            {
                try
                {
                    adapter.SaveEntityCollection(Ul01sCustomerEntityCollection);
                }
                catch (ORMConcurrencyException e)
                {
                    
                    
                    
                }
            

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 16-May-2016 19:44:47   

Are you sure this is the same code generating the exception? it seems like an Insert not an update, IsNew is not set for instance. It's one Save not twice.

As per your initial post, if you are on a subsequent saves, you should refetch the entity before re-saving it. (You can do the refetch whenever you save, by passing true for the refetch parameter in the correct Save overload method.

Luca75
User
Posts: 37
Joined: 10-Feb-2012
# Posted on: 16-May-2016 20:17:51   

Are you telling me that if I use the parameter = true refetch the first framework to save trying to retrieve the record from the database and shall do insert or update properly synchronizing the collaction with the database ? Thank you

Luca75
User
Posts: 37
Joined: 10-Feb-2012
# Posted on: 16-May-2016 20:21:21   

.. With refetch = true , the framework will do a job like this ? Thank you


// Define QuerySpec query
            var qf = new QueryFactory();
            var q = qf.Ul01sCustomer
                        .Where(Ul01sCustomerFields.CustomerId.Equal(MyDTOCustomer.CustomerId));
            EntityCollection<Ul01sCustomerEntity> customers;

            // fetch them using a DataAccessAdapter instance
            using (var adapter = new DataAccessAdapter())
            {
                customers = (EntityCollection<Ul01sCustomerEntity>)adapter.FetchQuery(q);
            }

            if (customers.Count == 1)
            {
                //Update
                customers[0].CompanyName = MyDTOCustomer.CompanyName;
                customers[0].ContactName = MyDTOCustomer.ContactName;
                customers[0].ContactTitle = MyDTOCustomer.ContactTitle;
                customers[0].Country = MyDTOCustomer.Country;
                return customers[0];
            }
            else
            {
                //Insert
            
                MyEntity =new Ul01sCustomerEntity();
                MyEntity.CustomerId = MyDTOCustomer.CustomerId;
                MyEntity.CompanyName = MyDTOCustomer.CompanyName;
                MyEntity.ContactName = MyDTOCustomer.ContactName;
                MyEntity.ContactTitle = MyDTOCustomer.ContactTitle;
                MyEntity.Country = MyDTOCustomer.Country;
                MyEntity.IsNew = true;
                return MyEntity;
            }
            //return customers[0];
        }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-May-2016 08:23:12   

Yes, in that code, the returned entity (be new or existing) will be ready for save. Then in that save you should put refetch=true to update the entity status.

David Elizondo | LLBLGen Support Team
Luca75
User
Posts: 37
Joined: 10-Feb-2012
# Posted on: 17-May-2016 09:46:51   

Ok Thanks