The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

esConcurrencyException on Update

Last post 07-06-2007, 4:58 PM by David.Parsons. 12 replies.
Sort Posts: Previous Next
  •  07-06-2007, 2:48 AM 3622

    esConcurrencyException on Update

    Hi,

     im using a DataGridView to display a View which uses fields from 3 different tables. When i try to update an Entity which corresponds to a single row from one of these 3 tables im getting an esConcurrencyException:

     EntitySpaces.Interfaces.esConcurrencyException was caught
      Message="Error in SqlClientProvider.esSaveDataTable"
      Source="EntitySpaces.Core"
      StackTrace:
           at EntitySpaces.Core.esEntity.SaveToProvider(esSqlAccessType sqlAccessType)
           at EntitySpaces.Core.esEntity.Save(esSqlAccessType sqlAccessType)
           at EntitySpaces.Core.esEntity.Save()

     The InnerException reads "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."
    Stacktrace:
       at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
       at EntitySpaces.SqlClientProvider.DataProvider.SaveStoredProc(esDataRequest request)
       at EntitySpaces.SqlClientProvider.DataProvider.EntitySpaces.Interfaces.IDataProvider.esSaveDataTable(esDataRequest request)


     
    Im not trying to edit in the GridView, im loading the Entity using a field from the View, updating one field and saving it. I don´t use any type of concurrency control.

     
    Any Ideas?

     
    Thanks in advance,

    Joris

     

  •  07-06-2007, 3:24 AM 3625 in reply to 3622

    Re: esConcurrencyException on Update

    "im loading the Entity using a field from the View, updating one field and saving it."

    Could you post the code for this?


    David Neal Parsons
    www.entityspaces.net
  •  07-06-2007, 3:30 AM 3626 in reply to 3625

    Re: esConcurrencyException on Update

    As per David's request, be sure to use the "code" button, it's the last button on the right when editing a post. This error is thrown only when no rows are updated, either you primary key is blank or the record you are updating was changed, it could also be your timestamp key was blank?

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  07-06-2007, 4:11 AM 3628 in reply to 3626

    Re: esConcurrencyException on Update

    While trying to put the code together i think i may have found the error. My pk consists of a string(lets say partnumber) and an integer-reference to another table (this table is for groups, so every partnumber can exist one time in every group).

    Im trying to update the reference to the group-table and then saving it. Should this be possible?

    I´ll make some tests and make a simple code-example if it doesn´t work. 

    I don´t use timestamps btw. 

  •  07-06-2007, 4:19 AM 3629 in reply to 3628

    Re: esConcurrencyException on Update

     

    Code:
                Connectors c = new Connectors();
    
                //PartNumber, GroupRef
                c.LoadByPrimaryKey("#04 003.321.011", 2);
    
                c.GroupRef = 14;
    
                c.Save();
     

     

    This doesn´t work. I guess there is way to do that though?
  •  07-06-2007, 4:34 AM 3630 in reply to 3629

    Re: esConcurrencyException on Update

    Views are not updateable, if this object is generated from a view?

    Regards,

    Scott Schecter
    EntitySpaces | My Site
  •  07-06-2007, 4:39 AM 3631 in reply to 3630

    Re: esConcurrencyException on Update

    Also, try this:

    Code:
    Connectors c = new Connectors();
    
    //PartNumber, GroupRef
    if(c.LoadByPrimaryKey("#04 003.321.011", 2))
    {
        c.GroupRef = 14;
        c.Save();
    }


    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  07-06-2007, 4:39 AM 3632 in reply to 3630

    Re: esConcurrencyException on Update

    You cannot update primary key fields.

    Code:
    Connectors oldOne = new Connectors();
    Connectors newOne = new Connectors();
    
    oldOne.LoadByPrimaryKey("#04 003.321.011", 2);
    newOne.AddNew();
    
    newOne.PartNumber = oldOne.PartNumber;
    newOne.GrouRef = 14;
    // copy other fields
    
    oldOne.MarkAsDeleted();
    
    using(esTransactionScope scope = new esTransactionScope())
    {
        oldOne.Save();
        newOne.Save();
    
        scope.Complete();
    }

    David Neal Parsons
    www.entityspaces.net
  •  07-06-2007, 4:44 AM 3633 in reply to 3630

    Re: esConcurrencyException on Update

    My above code example does not use views. "Connectors" is a table. My first posts here are kinda misleading now.

    Im trying to update a field which is part of the primary key. Thats not possible and i see why. I do have an identity column, too. That column is used for foreign keys in other tables, perhaps i should make that identity column the primary key. This would force me to validate my "old primary key" though...

  •  07-06-2007, 4:53 AM 3634 in reply to 3632

    Re: esConcurrencyException on Update

    Thanks and my apologizes for bringing that up, should have known better :)

    I would have to update some tables that are referencing that row if i keep that primary key. On the other hand i would not have validation when using my identity column...

    One death i have to take.

  •  07-06-2007, 5:30 AM 3636 in reply to 3634

    Re: esConcurrencyException on Update

    Now you have my curiosity piqued Wink

    You have an identity column, that is not the PK, and is part of a FK. I did not know that was even possible. Don't you run into a chicken and egg situation? You must save the parent before the child to avoid FK violations, but you must save the child before the parent in order to generate the identity column value that you need for the parent's PK.


    David Neal Parsons
    www.entityspaces.net
  •  07-06-2007, 5:50 AM 3639 in reply to 3636

    Re: esConcurrencyException on Update

    You missunderstood me i think, perhaps due to my bad english :)

    Lets say i have 3 fields in my "Connectors" table:

    ID - identity colum, autoincrement

    PartNumber - string, part1 of my primary key

    GroupRef - int, part2 of my primary key, and foreign key relating to my group table

     

    Other tables that are referencing this table do that with a foreign key that maps to "ID".

    In this case, i copy all fields to my new "Connector" except the Zero-To-Many fields. I save my new Connector, got a new "ID" and update every object that was referencing my old Connector.

    Works well by the way, but i fear any changes (new fields etc.) which could be forgotten.

    Will there be Clone()-Function? :)
     

  •  07-06-2007, 4:58 PM 3672 in reply to 3639

    Re: esConcurrencyException on Update

    Your English is excellent. Thx for clarifying re the identity column. As far as Clone, it is on our list for enhancements, but no release date has been set, yet.
    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML