-2

i have two objects parent and child (the same entity) and I need to update child object with all of properties (no key, of course) from parent, there are a lot of properties. is there a better way to execute it, Without update each field from parent to child? I tried automapper but no sucsess. Thanks!

c2s
  • 43
  • 1
  • 1
  • 4
  • Below is a good way, but i don't want a clone, i want update values from parent to child keeping their keys. http://stackoverflow.com/questions/15308747/entity-framework-5-deep-copy-clone-of-an-entity?answertab=votes#tab-top – c2s Apr 16 '15 at 18:13

1 Answers1

0

I didn't find a way to do that using EF, automapper generated some issues. Solution: I used reflection and get per type the properties that i would like to copy:

Dim listTypes As New List(Of Type)
                listTypes.Add(GetType(String))
                listTypes.Add(GetType(Date))
                listTypes.Add(GetType(Integer))
                listTypes.Add(GetType(Boolean))
                listTypes.Add(GetType(Nullable(Of Integer)))
                listTypes.Add(GetType(Nullable(Of Boolean)))
                listTypes.Add(GetType(Nullable(Of Date)))
Dim parentProperties = srParent.GetType().GetProperties.Where(Function(p) listTypes.Contains(p.PropertyType))
                For Each pp In parentProperties
                    If pp.Name.ToUpper() <> "SR_ID" AndAlso pp.Name.ToUpper() <> "SR_FK_SERVICE_REQUEST_PARENT" AndAlso pp.Name.ToUpper() <> "SR_BL_VISIBLE" Then
                        Dim value = pp.GetValue(srParent)
                        srChild.GetType().GetProperty(pp.Name).SetValue(srChild, value)
                    End If
                Next

`

c2s
  • 43
  • 1
  • 1
  • 4