How to map .NET class properties with same keys
usually, when we want to map 2 objects with same properties, we set a.key1 = b.key2 , a.key2 = b.key2 , so on....
there's a class in .NET that makes this easier ... System.Reflection
so in your C# class, put a using System.Reflection
then do something like below in your method. Mine is a sample where I am updating a class name SiteFeatures. Parameter is also of the same class, and I also have the id.
CODE STARTS HERE
public bool Edit(SiteFeature aSiteFeature, int id) { try { SiteFeature item = context.SiteFeatures.Where(b => b.SiteFeatureID == id).SingleOrDefault(); if (item != null) { // Iterate the Properties of the destination instance and // populate them from their source counterparts PropertyInfo[] destinationProperties = item.GetType().GetProperties(); foreach (PropertyInfo destinationPI in destinationProperties) { //dont include index PK if (destinationPI.Name == "SiteID") continue; PropertyInfo sourcePI = aSiteFeature.GetType().GetProperty(destinationPI.Name); destinationPI.SetValue(item, sourcePI.GetValue(aSiteFeature, null), null); } context.SaveChanges(); } return true; } catch (Exception ex) { return false; } }
CODE ENDS HERE










