Print Page | Close Window

getting an Entity Original and Current Versions

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2526
Printed Date: 05-May-2025 at 10:25am


Topic: getting an Entity Original and Current Versions
Posted By: vijayaleads
Subject: getting an Entity Original and Current Versions
Date Posted: 22-Feb-2011 at 1:19pm
Hello,

I know that we can find the Current and Original values of a property in an Entity using the GetValue method
For Example if my Entity is an Address with properties First Line,ZipCode and State

i can write something like

Address.PropertyMetaData.ZipCode.GetValue(address,EntityVersion.Current) and Address.PropertyMetaData.ZipCode.GetValue(address,EntityVersion.Original) to get the current and original ZipCode

But what I want to find out is if there is a way to get the Original and Current values of the Entity (Address) instead of a property?

Thanks for the help,
Vijaya




Replies:
Posted By: sbelini
Date Posted: 24-Feb-2011 at 12:53am

Hi Vijaya,

There's no specific method to accomplish that, but you could create one using reflection:
 
public Entity RetrieveEntity(Entity entity, EntityVersion version) {
  var entityType = entity.GetType();
  var resultEntity = Activator.CreateInstance(entityType);
  foreach (var entityProperty in EntityProperty.GetEntityProperties(entityType)) {
    if (entityProperty.GetType().Name.Contains(typeof(DataEntityProperty).Name)) {
      var value = ((DataEntityProperty)entityProperty).GetValue(entity, version);
      TypeDescriptor.GetProperties(resultEntity)[entityProperty.Name].SetValue(resultEntity, value);
    }
  }
  return (Entity)resultEntity;
}
 
However, note that the retrieved entity with the original or current values is a Detached entity with the same Id as the Entity you are trying to retrieve values from. (i.e. you shouldn't be trying to attach it to the EM)
 
Can you explain why you are trying to get the whole entity rather than the properties? Are trying to "roll back" the entity? (if that's the case the above wouldn't be the correct approach)
 
Please provide details on what you are trying to do, so I can better understand your issue and help you.
 
Silvio.



Print Page | Close Window