Da solche Funktionen erst mit EF5 bzw. .NET 4.5 halbwegs vernünftig zur Verfügung stehen ("AsNoTracking") hier eine Möglichkeit um Entitäten zu klonen (als Extension :-))
;
Public Function Clone(Of t As EntityObject)(entity As t, ctx As oraEntities, Optional copyKeys As Boolean = True) As t
Try
Dim cloneE As t = ctx.CreateObject(Of t)()
Dim pis As PropertyInfo() = entity.GetType.GetProperties()
For Each pi As PropertyInfo In pis
Dim attrs As EdmScalarPropertyAttribute() = DirectCast(pi.GetCustomAttributes(GetType(EdmScalarPropertyAttribute), False), EdmScalarPropertyAttribute())
For Each attr As EdmScalarPropertyAttribute In attrs
If Not copyKeys AndAlso attr.EntityKeyProperty Then
Continue For
End If
'Strung und NULL abfangen, wird trotz Standardwert im SQL Server nicht imgesetzt
Dim objVal As Object
If pi.PropertyType = GetType(String) And pi.GetValue(entity, Nothing) Is Nothing Then
objVal = ""
Else
objVal = pi.GetValue(entity, Nothing)
End If
pi.SetValue(cloneE, objVal, Nothing)
Next
Next
Return cloneE
Catch ex As Exception
Throw ex
End Try
End Function
und zu vergleichen
;
Public Function Compare(Of t As EntityObject)(entityOriginal As t, entityCopy As t) As Boolean
Try
'Um Änderungen einer Entität festzustellen (t.equals funktioniert aufgrund der Abhängigkeiten mit anderen Tabellen nicht)
Dim pisOrig As PropertyInfo() = entityOriginal.GetType.GetProperties()
For Each piOrig As PropertyInfo In pisOrig
Dim attrs As EdmScalarPropertyAttribute() = DirectCast(piOrig.GetCustomAttributes(GetType(EdmScalarPropertyAttribute), False), EdmScalarPropertyAttribute())
If attrs.Count > 0 Then 'Fremdschlüsseltabellen ausschliessen
Dim objOrig As Object = If(GetType(t).GetProperty(piOrig.Name).GetValue(entityOriginal, Nothing) Is Nothing, "", GetType(t).GetProperty(piOrig.Name).GetValue(entityOriginal, Nothing))
Dim objCopy As Object = If(GetType(t).GetProperty(piOrig.Name).GetValue(entityCopy, Nothing) Is Nothing, "", GetType(t).GetProperty(piOrig.Name).GetValue(entityCopy, Nothing))
If Not objOrig.Equals(objCopy) Then
Return False
End If
End If
Next
Return True
Catch ex As Exception
Throw ex
End Try
End Function
Verknüfte Tabellen werden dabei allerdings nicht übernommen!