对新EntityType变量更新源对象的更改

时间:2013-06-23 20:57:56

标签: entity-framework entity-framework-5

(更新:在初始代码块之后添加了解决方法)

我并没有声称任何错误,只是不明白这是如何运作的......

使用Visual Studio 2012 Express for Windows桌面和 实体框架5

我有一个新的Windows窗体应用程序,其中一个窗体FrmHome在app启动时运行。 我有一个名为GrowModel的ConceptualEntityModel。 我有一个名为Plant的EntityType。 工厂有两个属性ID和名称。 假设Plant中有一些记录。

由以下代码引发的消息框显示currentPlant.Name和plantCopy.Name的相同值(“XXX”)。我认为在声明plantCopy时使用New我会获得Plant对象的单独副本,而对plantCopy属性的更改不会影响currentPlant。

有人能否告诉我这是如何运作的?

谢谢!

Public Class FrmHome

    Private Shared m_growData As New GrowEntities

    Public Sub DoSomething() Handles Me.Load

        Dim currentPlant As Plant
        currentPlant = FindPlantById(1)

        Dim plantCopy as New Plant
        plantCopy = currentPlant

        plantCopy.Name = "XXX"

            MessageBox.Show("Current Plant Name: " & _
            currentPlant.Name & ControlChars.CrLf & _
            "Plant Copy Name:" & plantCopy.Name)

    End Sub



    Shared Function FindPlantById(ByVal PlantId As Integer) As Plant

        Dim PlantList As New List(Of Plant)
        PlantList = m_growData.Plants.ToList

        Dim intPlantLoopCount As Integer = PlantList.Count - 1

        For y = 0 To intPlantLoopCount

            Dim currentPlant As Plant = PlantList(y)

            If currentPlant.Id = PlantId Then
                Return currentPlant
            End If

        Next

        Return Nothing

    End Function

End Class

(更新1)

目前我正在使用下面的代码作为解决方法,因为您可以复制工厂的元素而不传递引用。注意添加的CopyPlant函数,它在为plantCopy赋值时使用。假设Plant的两个附加属性名为Notes和LastUpdate。使用下面的代码,plantCopy.Name是'XXX',currentPlant.Name是“OriginalValue”。仍然看起来你应该能够在没有参考的情况下将一个植物复制到另一个植物但是如果我必须做额外的工作那么我会。

Public Class FrmHome

    Private Shared m_growData As New GrowEntities

    Public Sub DoSomething() Handles Me.Load

        Dim currentPlant As Plant
        currentPlant = FindPlantById(1)

        Dim plantCopy as New Plant
        plantCopy = CopyPlant(currentPlant)

        plantCopy.Name = "XXX"

            MessageBox.Show("Current Plant Name: " & _
            currentPlant.Name & ControlChars.CrLf & _
            "Plant Copy Name:" & plantCopy.Name)

    End Sub


    Shared Function FindPlantById(ByVal PlantId As Integer) As Plant

        Dim PlantList As New List(Of Plant)
        PlantList = m_growData.Plants.ToList

        Dim intPlantLoopCount As Integer = PlantList.Count - 1

        For y = 0 To intPlantLoopCount

            Dim currentPlant As Plant = PlantList(y)

            If currentPlant.Id = PlantId Then
                Return currentPlant
            End If

        Next

        Return Nothing

    End Function

    Shared Function CopyPlant(ByVal source As Plant) As Plant

        Dim target As New Plant

        target.Name = source.Name
        target.Notes = source.Notes
        target.LastUpdate = Now()

        Return target

    End Function

End Class

1 个答案:

答案 0 :(得分:0)

您正在覆盖引用,以便plantCopy和currentPlant都引用同一个对象,替换您使用New创建的Plant的单独副本。

您可以使用http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx克隆对象,而不是复制引用,或搜索许多类似的问题。