需要从数据库查询的.NET属性(通过实体框架)

时间:2014-05-09 06:23:23

标签: .net wpf entity-framework binding

我正在尝试将属性定义为我的WPF应用程序可以绑定到的(EntityObject)部分类。我不确定如何编写该属性,因为它必须查询和更新EntityModel /数据库中的值。例如,当我在DataGrid中进行这些绑定时,我发现性能显着下降。

示例:我想从数据库中获取值到TextBox中,并在编辑时将其推送回数据库。

<TextBox Text="{Binding SelectedItem.PurchaseCost, Mode=TwoWay}" />

财产:

Partial Public Class Item

    Public Property PurchaseCost As Double
        Get
            If _PurchaseCost Is Nothing Then
                Dim query = From a In Me.Assumptions
                            Where a.AssumptionID = 132 And a.ItemID = Me.Id
                            Select a
                _PurchaseCost = query.FirstOrDefault()

                If _PurchaseCost Is Nothing Then
                    Return Nothing
                Else
                    Return CDbl(_PurchaseCost.value)
                End If
            Else
                Return CDbl(_PurchaseCost.value)
            End If
        End Get
        Set(ByVal value As Double)
            If _PurchaseCost Is Nothing Then
                _PurchaseCost = New Assumption With {.AssumptionID = 132, .ItemID = Me.Id, .Value = value}
                Me.Assumptions.Add(_PurchaseCost)
            Else
                _PurchaseCost.value = value
            End If
        End Set
    End Property
    Private _PurchaseCost As Assumption

End Class

EntityObject Assumption

AssumptionID  ItemID   Value
     131         1      100
     132         1      300
     133         1      150    

EntityObject Item与EntityObject Assumption具有1对多的关系,我知道我正在寻找的属性PurchaseCost存储在AssumptionID = 132的数据库中。

可以更有效地将数据库中的值绑定到TextBox中吗?

0 个答案:

没有答案