Clone Entity RRS feed

  • Question

  • Good night

    I am using the ef 4.1 in code first and need to clone my entity and insert the cloned entity in the database, but when i try get database error which says that fk exists. To clone entity i use datacontractserializer.

    Its possible to make the clone of entities? Now if yes how and what I can do?

    Thank you

    Private Shared Function Clone(Of T)(obj As T) As T
    	Dim dcSer As New DataContractSerializer(obj.[GetType]())
    	Dim memoryStream As New MemoryStream()
    	dcSer.WriteObject(memoryStream, obj)
    	memoryStream.Position = 0
    	Dim newObject As T = DirectCast(dcSer.ReadObject(memoryStream), T)
    	Return newObject
    End Function

    Wednesday, June 20, 2012 9:28 PM


All replies