How to Define your Audits

There are a few different ways to enlist an entity for auditing, with varying degrees of flexibility.

Use your DataContext table properties

This is the best way to create audit definitions, and should handle 95% of use cases. This logic relies on your DataContext having properties for each of your tables. It will use attribute reflection to look up the primary keys of each table.

Alternatively, you can explicitly define your primary key fields if you do not want to rely on reflection, or if you have any problems with the implicit method.
dataContext.Products.Audit(p => p.ProductID);
dataContext.Categories.Audit(c => c.CategoryID);
dataContext.Orders.Audit(o => o.OrderID);

Specify the Entity Type manually

Use of this method will be rare, but it will come in handy if your DataContext does not have properties to access your tables.

You can also use this method to define the primary keys.
dataContext.Audit<Product>(p => p.ProductID);
dataContext.Audit<Category>(c => c.CategoryID);
dataContext.Audit<Order>(o => o.OrderID);

How to Audit Associations

You will probably want to audit some "children" relationships eventually. This will come in handy if you want to Audit the Orders table, but you also want to audit all OrderDetails that are related to the Order.

Use the Association Property of your Entity

This is the best way to audit associations, but it assumes that you have an IEnumerable property on your Entity to access the association.
       .AuditAssociation(o => o.OrderDetails);

       .AuditAssociation(c => c.Addresses)
       .AuditAssociation(c => c.PhoneNumbers);

Explicity Map the PK/FK Properties

This method will come in handy if you do not have an association property on your entity to rely on
       .AuditAssociation<OrderDetail>(od => od.OrderDetailID, od => od.OrderID);

       .AuditAssociation<Address>(a => a.AddressID, a => a.ContactID)
       .AuditAssociation<PhoneNumber>(p => p.PhoneNumberID, p => p.ContactID);

Last edited Jan 12, 2009 at 5:51 PM by mhidinger, version 4


Florens Sep 3, 2009 at 1:17 PM 
what would be the best way to audit multilevel associations? ie. Store->Ailes->Shelves->Goods
As it is now you get double entries if you try this:

the other option would be:

but this does not register the link between ailes and shelves.