Hi all,
I have the following extension method and i'm not sure why each entrys state is being changed to Unchanged after i call entry.AcceptChanges() on one entry.
public static void SaveWithLogging(this ObjectContext context)
{
IEnumerable<ObjectStateEntry> entries = context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added | EntityState.Deleted);
foreach (var entry in entries)
CreateTransactionLog(context, entry);
}
private static void CreateTransactionLog(ObjectContext context, ObjectStateEntry entry)
{
using (TransactionScope transaction = new TransactionScope())
{
string operationType = entry.State.ToString();
context.SaveChanges(SaveOptions.DetectChangesBeforeSave);
if (entry.State == EntityState.Added) entry.AcceptChanges();
var columnNames = (from p in entry.EntitySet.ElementType.Members
select p.Name)
.ToList();
Log log = new Log();
log.CreateDate= DateTime.Now;
log.UserName = "Test";
log.Operation = operationType;
context.AddObject("Logs", log);
foreach (var columnName in columnNames)
{
string oldValue = entry.State == EntityState.Added ? string.Empty : entry.OriginalValues[columnName].ToString();
string newValue = entry.CurrentValues[columnName].ToString();
if (oldValue.CompareTo(newValue) != 0)
{
// Create Log Details
LogDetail logDetails = LogDetail();
logDetails.LogId = log.LogId;
logDetails.TableName = entry.EntitySet.Name.ToString();
logDetails.Field = columnName.ToString();
logDetails.Before = oldValue;
logDetails.After = newValue;
logDetails.PKValue = entry.CurrentValues[0].ToString();
context.AddObject("LogDetails", logDetails);
}
}
context.SaveChanges();
transaction.Complete();
}
}
Am i doing something incorrectly?
Thanks