How to copy properties from one .net object to another
<p>I was trying to find a way to save the object posted to a web service to a database managed by entity. </p>

<p>Instead of copying each property manually, I wanted a way to copy all properties without me doing much code. </p>

<p>Ex: objectFromClient.Copy(objectToDatabase);</p>

<p>This would save me multiple lines of code to copy each property. I liked the suggested code given in this question.</p>

<p><a href="https://stackoverflow.com/questions/930433/apply-properties-values-from-one-object-to-another-of-the-same-type-automaticall">Apply properties values from one object to another of the same type automatically?</a></p>

<p>However this didnt work on objects tracked by Entity since the Key properties can not be modified in Entity.</p>

<p>I made some amendments to skip those columns which are tagged as EntityKey. </p>

<p>I am not sure if this was a correct approach. Can somebody comment?</p>

<pre><code>using System;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Reflection;
/// &lt;summary&gt;
/// A static class for reflection type functions
/// &lt;/summary&gt;
public static class Reflection
{
    /// &lt;summary&gt;
    /// Extension for 'Object' that copies the properties to a destination object.
    /// &lt;/summary&gt;
    /// &lt;param name="source"&gt;The source.&lt;/param&gt;
    /// &lt;param name="destination"&gt;The destination.&lt;/param&gt;
    public static void CopyProperties(this object source, object destination)
    {
        // If any this null throw an exception
        if (source == null || destination == null)
            throw new Exception("Source or/and Destination Objects are null");
        // Getting the Types of the objects
        Type typeDest = destination.GetType();
        Type typeSrc = source.GetType();
        // Collect all the valid properties to map
        var results = from srcProp in typeSrc.GetProperties()
                      let targetProperty = typeDest.GetProperty(srcProp.Name)
                      where srcProp.CanRead
                      &amp;&amp; targetProperty != null
                      &amp;&amp; (targetProperty.GetSetMethod(true) != null &amp;&amp; !targetProperty.GetSetMethod(true).IsPrivate)
                      &amp;&amp; (targetProperty.GetSetMethod().Attributes &amp; MethodAttributes.Static) == 0
                      &amp;&amp; targetProperty.PropertyType.IsAssignableFrom(srcProp.PropertyType)
                      &amp;&amp; targetProperty.GetCustomAttributes(false).Where(a =&gt; a is EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() == 0
                      &amp;&amp; srcProp.Name != "EntityKey" 
                      select new { sourceProperty = srcProp, targetProperty = targetProperty };
        //map the properties
        foreach (var props in results)
        {
            //System.Diagnostics.Debug.WriteLine(props.targetProperty.Name);
            props.targetProperty.SetValue(destination, props.sourceProperty.GetValue(source, null), null);
        }
    }
}
</code></pre>