Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here was the final solution.</p> <p>I basically check my model for the presence of 4 properties. If the entity contains</p> <p>InsertedOn and InsertedBy and UpdatedOn and UpdatedBy I generate (using a tt file) the following entity that implements an IAuditable interface.</p> <pre><code>public interface IAuditable { void SetInsertedOn(DateTime date); void SetInsertedBy(string user); void SetUpdatedOn(DateTime date); void SetUpdatedBy(string user); } public partial class ExampleDBtable: EntityObject, Audit.IAuditable { //Normal EDMX stuff here.. public static ExampleDBtable CreateExampleDBtable(int id, string code, string name, DateTime insertedOn, string insertedBy, DateTime updatedOn, string updatedBy) { } //Extension points. #region IAuditable Members public void SetInsertedOn(DateTime date) { this._InsertedOn = date; } public void SetInsertedBy(string user) { this._InsertedBy = user; } public void SetUpdatedOn(DateTime date) { this._UpdatedOn = date; } public void SetUpdatedBy(string user) { this._UpdatedBy = user; } #endregion } </code></pre> <p>I also generate code to handle the updating of audit information</p> <pre><code> public ExampleDBObjectContext(string connectionString) : base(connectionString, "publicExampleDBObjectContext") { this.SavingChanges += new EventHandler(UpdateAuditInformation); this.OnContextCreated(); } </code></pre> <p>And then finally I implemented the UpdateAuditInformation</p> <pre><code> foreach (ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries( EntityState.Added | EntityState.Modified)) { //Start pseudo code.. if(entry.Entity is IAuditable){ IAuditable temp = entry.Entity as IAuditable; temp.SetInsertedOn(DateTime.Now); temp.SetInsertedBy(Env.GetUser()); ... } } </code></pre> <p>I opted not to show the .tt file because it looks horrible in the editor.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload