I am trying to clone an entity as part of an auditing process. Because the IsChanged value gets reset to false, I want to loop through the fields and set that flag based on a comparison between CurrentValue and DbValue. I tried the comparison below but it blows up when one of the values does not exist. I tried checking against null and DbNull.Value with the same results.
What is the best way for me to do this?
var eType = (EntityType)Enum.Parse(typeof(EntityType), entity.LLBLGenProEntityName);
var clonedEntity = GeneralEntityFactory.Create((EntityType entity.LLBLGenProEntityTypeValue);
//fill the field data in the cloned entity
clonedEntity.Fields = inputEntity.Fields.Clone();
// set the field state to fetched to prevent ORMEntityOutOfSyncException
clonedEntity.Fields.State = EntityState.Fetched;
//after setting the field state to fetched, all the fields show as not changed
//do a loop through the fields and set IsChanged to correct status
foreach (IEntityField2 field in clonedEntity.Fields)
{
field.IsChanged = !field.CurrentValue.Equals(field.DbValue);
}