[dba-VB] CodeFirst, DataBinding and audit trailing deleted items on client side

Scott Marcus scott.marcus at tsstech.com
Tue Apr 16 08:16:28 CDT 2013


I guess I'm not sure what you mean by individual changes. SaveChanges should happen at your whim and whether it is to one record or many records is your call. Perhaps there is a misunderstanding since I am using an MVC repository method in ASP.NET. I just thought entity framework behaves the same regardless the environment.

Here is an example...

    public class MyModelRepository : IMyModelRepository
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<MyModel> MyModels
        {
            get { return context.MyModels; }
        }

        public void Save(MyModel someRecord, int loginID, string changesMade = "Record Created")
        {
            DateTime changesMadeDateAndTime = DateTime.Now;
            AuditModel auditRecord = new AuditModel
            {
                WhoDoneIt = loginID,
                WhatDidTheyDo = changesMade,
                WhenDidTheyDoIt = changesMadeDateAndTime
            };
            context.AuditRecordModels.Add(auditRecord);

           // if you wanted, you could add more records and change more records before you call SaveChanges
           //       you would probably not do it in this function but in another overloaded function
           //       that handles multiple record changes

            context.SaveChanges();  //saves the changes to someRecord and adds the auditRecord all in one transaction
        }

        public void Delete(MyModel someRecord, int loginID)
        {
            DateTime deletedDateAndTime = DateTime.Now;
            AuditModel auditRecord = new AuditModel
            {
                WhoDoneIt = loginID,
                WhatDidTheyDo = "Deleted MyModel Record: " + someRecord.RecordID,
                WhenDidTheyDoIt = deletedDateAndTime
            };
            context.AuditRecordModels.Add(auditRecord);
            context.MyModels.Remove(someRecord);
            context.SaveChanges(); //now perform the work that adds the audit record and removes someRecord all in one transaction
        }
    }


Scott

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com [mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Salakhetdinov Shamil
Sent: Tuesday, April 16, 2013 4:40 AM
To: Discussion concerning Visual Basic and related programming issues.
Subject: Re: [dba-VB] CodeFirst, DataBinding and audit trailing deleted items on client side

 Hi Scott --

I actually have to look for

EntityState.Modified,  EntityState.Added and EntityState.Deleted

which are "automagically" set "behind the scene" for edited items while data is edited in a DataGridView bound to DBSet<MyTestEntity>.Local

Here is a test code I use:

MyTestEntitiesContainer _context;
IOrderedQueryable<MyTestEntity> _list;

private void loadData()
{
   _context = new MyTestEntitiesContainer();
   _list = from s in _context.MyTestEntity orderby s.Name select s;
   _list.Load();
    myTestEntityBindingSource.DataSource = _context.MyTestEntity.Local; }

As it happens (if I'm not mistaked) items with EntityState.Modified and  EntityState.Added can be found in _context.MyTestEntity.Local but items with EntityState.Deleted can be only found in _list.

And as I have noted I wanted to get audit trailing facility to work on currently edited (changed, added, deleted) item not by investigating the whole _context.MyTestEntity.Local or _list.

Anyway it's not urgent issue. I will check it more n the coming days. Your comments are very welcome.

Thank you.

-- Shamil


Понедельник, 15 апреля 2013, 18:50 -04:00 от Scott Marcus <tsm at zoomtown.com>:
>Before calling the save changes method, add the audit record to the context and change its state to modified. Then call the save changes method.
>
>i.e. (in c#)
>
>Audit audit = new Audit({UserName = username, RecordID = 
>changedRecordID}); context.Entry(audit).State = EntityState.Modified; 
>context.SaveChanges;
>
>Sent from my iPad
>
>On Apr 15, 2013, at 4:39 PM, Salakhetdinov Shamil < mcp2004 at mail.ru > wrote:
>
>> Hi Scott --
>> 
>> I'm trying to audit trail *individual* changes while the rows' editions, additions, deletions are done using a DataGridView bound to a DbSet<{{EntityType}}>  of a {{CustomModel}}Container: DbContext IOW before they get saved/committed to the backend.
>> 
>> I have set custom events processing for the BidindingSource.ListChanged event and for the DataGridView.RowsRemoved event. They together allow me to audit trail changes on client side. But I suppose this custom audit trail solution is a bit tricky to be true - there should be a simpler one?
>> 
>> I can also audit trail the changes in a "batch" mode before they get saved to the backend by using .SaveChanges method of a {{CustomModel}}Container: DbContext then custom events processing of the BidindingSource.ListChanged event and of the DataGridView.RowsRemoved isn't needed. But I'd prefer to have an option to audit trail *individual* changes using simpler solution than I have currently got working.
>> 
>> I must note that the subject issue isn't currently needed here to be solved for any real life project - it's more a "research" kind of issue helping to learn better how bound WinForms communicate with their "model first" datasources...
>> 
>> Thank you.
>> 
>> -- Shamil
>> 
>> 
>> Понедельник, 15 апреля 2013, 10:50 -04:00 от Scott Marcus < scott.marcus at tsstech.com >:
>>> Where are you trying to perform the audit routines? Are you doing the audit changes after saving the changes to your model. Ideally, you would be making the audit records within the same savechanges context (since all changes are within the same transaction) so that any error to the savechanges does not create an audit of the changes that were rolled back.
>>> 
>>> -----Original Message-----
>>> From:  dba-vb-bounces at databaseadvisors.com 
>>> [mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of 
>>> Salakhetdinov Shamil
>>> Sent: Monday, April 15, 2013 7:23 AM
>>> To: Discussion concerning Visual Basic and related programming issues.
>>> Subject: [dba-VB] CodeFirst, DataBinding and audit trailing deleted 
>>> items on client side
>>> 
>>>  Hi All --
>>> 
>>> I have made a sample app here using CodeFirst ADO.NET EF approach. It works well. I just cannot currently solve one issue, namely:
>>> 
>>> - to audit trail items deleted in a bound DataGridView.
>>> 
>>> I have found how to find deleted items in a bound DbSet<{{EntityType}} list but the issue is that deleted item gets its item Id (surrogate key) zeroed, so my audit trailing can't be used to find corresponding database record.
>>> 
>>> Any ideas?
>>> 
>>> Thank you.
>>> 
>>> -- Shamil
>>> _______________________________________________
>>> dba-VB mailing list
>>>  dba-VB at databaseadvisors.com
>>>  http://databaseadvisors.com/mailman/listinfo/dba-vb
>>>  http://www.databaseadvisors.com
>>> 
>>> 
>>> NOTICE:  This electronic mail transmission is for the use of the named individual or entity to which it is directed and may contain information that is privileged or confidential.  If you are not the intended recipient, any disclosure, copying, distribution or use of the contents of any information contained herein is prohibited.  If you have received this electronic mail transmission in error, delete it from your system without copying or forwarding it, and notify the sender of the error by replying via email or calling TSS Technologies at (513) 772-7000, so that our address record can be corrected.
>>> Any information included in this email is provided on an “as is” and “where as” basis, and TSS Technologies makes no representations or warranties of any kind with respect to the completeness or accuracy of the information contained in this email.
>> 
>> _______________________________________________
>> dba-VB mailing list
>>  dba-VB at databaseadvisors.com
>>  http://databaseadvisors.com/mailman/listinfo/dba-vb
>>  http://www.databaseadvisors.com
>> 

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list