Here is the code to do the same thing than in my previous article “Bring a bit of the Subsonic power to Linq to sql by adding automatic audit and logical delete fields”
namespace YouNamespace.DAL
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Common;
using System.Data.Objects;
using System.Linq;
public partial class YOURCONTEXTEntities
{
/// <summary>
/// System fields for automatic audit and logical delete
/// </summary>
private struct SystemFields
{
public const string CreatedOn = "CREATEDON";
public const string ModifiedOn = "MODIFIEDON";
public const string CreatedBy = "CREATEDBY";
public const string ModifiedBy = "MODIFIEDBY";
public const string IsDeleted = "ISDELETED";
}
/// <summary>
/// Overriding the SaveChanges method to automaticaly set system fields if any.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public override int SaveChanges(System.Data.Objects.SaveOptions options)
{
IEnumerable<ObjectStateEntry> newEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added);
foreach (ObjectStateEntry entry in newEntries)
{
ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
.DataRecordInfo.FieldMetadata;
FieldMetadata createdOnField = fieldsMetaData
.Where(f => string.Equals(f.FieldType.Name, SystemFields.CreatedOn, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (createdOnField.FieldType != null)
{
entry.CurrentValues.SetValue(createdOnField.Ordinal, DateTime.Now);
}
FieldMetadata createdByField = fieldsMetaData
.Where(f => string.Equals(f.FieldType.Name, SystemFields.CreatedBy, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (createdByField.FieldType != null)
{
entry.CurrentValues.SetValue(createdByField.Ordinal, "Sam");
}
FieldMetadata deletedField = fieldsMetaData
.Where(f => string.Equals(f.FieldType.Name, SystemFields.IsDeleted, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (deletedField.FieldType != null)
{
entry.CurrentValues.SetValue(deletedField.Ordinal, false);
}
}
IEnumerable<ObjectStateEntry> modifiedEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
foreach (ObjectStateEntry entry in modifiedEntries)
{
ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
.DataRecordInfo.FieldMetadata;
FieldMetadata createdOnField = fieldsMetaData
.Where(f => string.Equals(f.FieldType.Name, SystemFields.ModifiedOn, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (createdOnField.FieldType != null)
{
entry.CurrentValues.SetValue(createdOnField.Ordinal, DateTime.Now);
}
FieldMetadata createdByField = fieldsMetaData
.Where(f => string.Equals(f.FieldType.Name, SystemFields.ModifiedBy, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (createdByField.FieldType != null)
{
entry.CurrentValues.SetValue(createdByField.Ordinal, "Sam");
}
}
IEnumerable<ObjectStateEntry> deletedEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted);
foreach (ObjectStateEntry entry in deletedEntries)
{
// change from deleted to modified (!important)
this.ObjectStateManager.ChangeObjectState(entry.Entity, EntityState.Modified);
ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
.DataRecordInfo.FieldMetadata;
FieldMetadata deletedField = fieldsMetaData
.Where(f => string.Equals(f.FieldType.Name, SystemFields.IsDeleted, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (deletedField.FieldType != null)
{
entry.CurrentValues.SetValue(deletedField.Ordinal, true);
}
else
{
// change back from modified to deleted (!important)
this.ObjectStateManager.ChangeObjectState(entry.Entity, EntityState.Deleted);
}
}
return base.SaveChanges(options);
}
}
}







