none
Conncurrency issues with Audtiing Fields RRS feed

  • Question

  • I am using  XXinsrting and XXXXupdataing to set values for auditing (createdate updatedate CreatedBy Updated By) this is causing concurrency issues.

    The issues arrise when I save a record and then immediately try to delete it  the create/update dates throw errors.

    partial void Customers_Inserting(Customer entity)
            {
                 entity.DateCreate = DateTime.Now;
                entity.DateUpdate = DateTime.Now;
                entity.CreatedBy = Application.User.FullName;
                entity.UpdatedBy = Application.User.FullName;
    
            }
    
            partial void Customers_Updating(Customer entity)
            {
               
                entity.DateUpdate = DateTime.Now;
                entity.UpdatedBy = Application.User.FullName;
            }
    


    Short of calling XXXCollection.Refresh() on the saved event I have not been able to get around this.

    Ideas??

     

     


    • Edited by rdavison Saturday, November 5, 2011 5:31 PM
    Saturday, November 5, 2011 5:31 PM

Answers

  • You can look in this post for an elegant way to do it at the client.
    1+1 = 3 for large values of 1
    • Marked as answer by rdavison Monday, November 21, 2011 2:09 AM
    Sunday, November 20, 2011 11:07 AM
  • I think you would have to refresh the collection before you can delete a changed record. Entity Framework may not be able to complete the second action with the first one still "pending".

    Yann - LightSwitch Central

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by rdavison Monday, November 7, 2011 3:16 AM
    Sunday, November 6, 2011 6:04 AM
    Moderator
  • I have faced this issue, and my conclusion is that the only way to handle is remove concurrency check for these fields that are "automatically" modified. The other approach that is not appealing AT ALL is move the logic to the form's saving partial method, so as the client record is up to date with the changes. But this is clearly a patch. If you are interested in the first approach you can take a look here and here. RIA services is the name of the game :-).

    The issue is that when you modify auditing fields the record at the client still holds the old values, so when you try to Update or Delete the record sends these old values that are used for concurency checking to the server. But the values at the DB are up to date causing the concurrency issue.


    1+1 = 3 for large values of 1
    Sunday, November 6, 2011 9:12 AM

All replies

  • I think you would have to refresh the collection before you can delete a changed record. Entity Framework may not be able to complete the second action with the first one still "pending".

    Yann - LightSwitch Central

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by rdavison Monday, November 7, 2011 3:16 AM
    Sunday, November 6, 2011 6:04 AM
    Moderator
  • I have faced this issue, and my conclusion is that the only way to handle is remove concurrency check for these fields that are "automatically" modified. The other approach that is not appealing AT ALL is move the logic to the form's saving partial method, so as the client record is up to date with the changes. But this is clearly a patch. If you are interested in the first approach you can take a look here and here. RIA services is the name of the game :-).

    The issue is that when you modify auditing fields the record at the client still holds the old values, so when you try to Update or Delete the record sends these old values that are used for concurency checking to the server. But the values at the DB are up to date causing the concurrency issue.


    1+1 = 3 for large values of 1
    Sunday, November 6, 2011 9:12 AM
  • Yann, Kostas

    Thanks for the comments.

    Yann that was my experience as well had to put a collection.refresh() for each collection on the screen in the save method of the screen.

    Kostas, I wouldn't mind doing a RIA service so much if I could do it *code first*. I have made it work in the past but tried from scratch today and it would not work. not sure why.

    For now since my data set will be small I am not to hurt by the refresh  but when and if code first can be made to work.. or if i get time to figure out how to do EF pocos that will be the way to go.

    Thanks again

    Monday, November 7, 2011 3:16 AM
  • I see your point

    The truth is EF although trying hard has more way to go to support Code First properly, or to be more fair, more effectively. This is the big trade off with RIA Services, especially in LightSwitch, since they can solve many issues that Native and Sql Server Data sources cannot handle, but the maitenance of domain service classes and EF models is an issue. Especially (which is 99% of the cases I believe) if you have a frequently updated domain model.


    1+1 = 3 for large values of 1
    Monday, November 7, 2011 7:30 AM
  • You can look in this post for an elegant way to do it at the client.
    1+1 = 3 for large values of 1
    • Marked as answer by rdavison Monday, November 21, 2011 2:09 AM
    Sunday, November 20, 2011 11:07 AM
  • Awesome post.

    Will try it when I get a chance.

    Monday, November 21, 2011 2:08 AM
  • I am using  XXinsrting and XXXXupdataing to set values for auditing (createdate updatedate CreatedBy Updated By) this is causing concurrency issues.

    The issues arrise when I save a record and then immediately try to delete it  the create/update dates throw errors.

    partial void Customers_Inserting(Customer entity)
            {
                 entity.DateCreate = DateTime.Now;
                entity.DateUpdate = DateTime.Now;
                entity.CreatedBy = Application.User.FullName;
                entity.UpdatedBy = Application.User.FullName;
    
            }
    
            partial void Customers_Updating(Customer entity)
            {
               
                entity.DateUpdate = DateTime.Now;
                entity.UpdatedBy = Application.User.FullName;
            }
    


    Short of calling XXXCollection.Refresh() on the saved event I have not been able to get around this.

    Ideas??

     

     


    I think you can avoid the trouble by NOT storing audit info in the table being audited.

    I described some time ago a potential approach here: http://blog.pragmaswitch.com/?p=8#comments

    paul.


    paul van bladel
    Monday, November 21, 2011 6:08 AM