This is a migrated thread and some comments may be shown as answers.

Audit Data Feature

3 Answers 51 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
john
Top achievements
Rank 1
john asked on 13 Dec 2012, 03:05 AM
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.

3 Answers, 1 is accepted

Sort by
0
Dimitar Tachev
Telerik team
answered on 17 Dec 2012, 04:22 PM
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.
0
john
Top achievements
Rank 1
answered on 18 Dec 2012, 01:29 AM
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.
0
Ivailo
Telerik team
answered on 18 Dec 2012, 07:41 AM
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.
Tags
General Discussions
Asked by
john
Top achievements
Rank 1
Answers by
Dimitar Tachev
Telerik team
john
Top achievements
Rank 1
Ivailo
Telerik team
Share this question
or