Cloning an entity/testing for field value

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 21-Mar-2012 13:00:59   

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); 
}


Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Mar-2012 13:47:08   

Since you are looping on the fields of the destination entity, why don't you use the field name to access the same field in the source entity, and hence copy its IsChanged value?

jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 21-Mar-2012 18:09:49   

That is a good suggestion and I can do that. I'd still like to know what the CurrentValue or DbValue evaluate to when they are blank in the XML.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Mar-2012 05:02:37   
foreach (IEntityField2 field in clonedEntity.Fields)
{
    field.IsChanged = !(field.CurrentValue != null && field.CurrentValue.Equals(field.DbValue)); 
}
David Elizondo | LLBLGen Support Team