Audit Data Feature

4 posts, 0 answers
  1. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 12 Dec 2012 Link to this post

    Could you help to suggest whether there is a easy to implement following requirement with OpenAccess ORM, SQL SERVER 2012?
    Record all inserted/updated record in one transaction for following information:
    Inserted Record: all data after inserted
    Updated Record: all data before updated, all data after updated.
  2. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 17 Dec 2012 Link to this post

    Hi John,

    In order to get the changes that will be performed during the next commit you could use the GetChanges method of your context instance. For your convenience I prepared the following code snipped demonstrating that approach. 

    using (EntitiesModel context = new EntitiesModel())
    {
                 // set this property if you need to check the original value of some of your records
        context.ContextOptions.MaintainOriginalValues = true;
     
        // perform some CRUD operations over your data here
                 ...
     
        try
        {
             // getting the changes
          Telerik.OpenAccess.ContextChanges contextChanges = context.GetChanges();
      
              // getting the new records
          IList<SomeEntity> inserts = contextChanges.GetInserts<SomeEntity>();
              // getting the records that will be updated
          IList<SomeEntity> updates = contextChanges.GetUpdates<SomeEntity>();
              // getting the records that will be deleted
          IList<SomeEntity> deletes = contextChanges.GetDeletes<SomeEntity>();
      
              // try to commit the transaction
           context.SaveChanges();
      
             // log the inserts, updates and deletes from the above lists here
     
             // if you need to check the original value before being updated
             foreach (var item in updates)
          {
                 // getting the original value of “someProperty”
              object originalValue =
                                   db.GetOriginalValue<
    object>(item, "SomeProperty");
            
        }
        catch (Exception)
        {
               // cancel the transaction
            context.ClearChanges();
     
            throw;
        }
    }


    Below you could find more useful information about handling transactions and accessing original values.
    - Handling transactions and operating with the pending changes.
    - Accessing original values after changes.

    I hope this helps. Let me know if you need additional directions/assistance.

    Regards,
    Dimitar Tachev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
  4. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 17 Dec 2012 Link to this post

    Hi,

    I think you can read http://www.simple-talk.com/sql/learn-sql-server/introduction-to-change-data-capture-(cdc)-in-sql-server-2008/
    I need the above solution. I think telerik also can add the similar solution in future.
  5. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 18 Dec 2012 Link to this post

    Hello John,

    You are right that this is one possible direction for further simplifying your scenario.

    However, bear in mind that a customization like this one is possible even now with OpenAccess ORM, as the changes are mostly in the database and can be done manually. Also you can achieve similar results with triggers or stored procedures.

    Let us know if you have any additional feedback.

    Regards,
    Ivailo
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Back to Top