SubmitChanges不会更新数据库的外键

时间:2015-01-11 21:31:13

标签: vb.net linq silverlight windows-phone

我的问题出现在Windows Phone 8.1 Silverlight VB应用程序中。 CatDataContext定义了一个表Books with items Title和一个外键_seriesID,属于一个表Series。

   <Table()>
    Public Class Series
        Implements INotifyPropertyChanged, INotifyPropertyChanging
        ' Define ID: private field, public property, and database column.
        Private _seriesID As Integer

        <Column(IsPrimaryKey:=True, IsDbGenerated:=True, DbType:="INT NOT NULL Identity", CanBeNull:=False,
            AutoSync:=AutoSync.OnInsert)>
        Public Property SeriesID() As Integer
            Get
                Return _seriesID
            End Get
            Set(ByVal value As Integer)
                If _seriesID <> value Then
                    NotifyPropertyChanging("SeriesID")
                    _seriesID = value
                    NotifyPropertyChanged("SeriesID")
                End If
            End Set
        End Property

        ' Define name: private field, public property, and database column.
        Private _Name As String
        <Column()>
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                If _Name <> value Then
                    NotifyPropertyChanging("Name")
                    _Name = value
                    NotifyPropertyChanged("Name")
                End If
            End Set
        End Property

#Region "INotifyPropertyChanged Members"

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        ' Used to notify that a property changed
        Private Sub NotifyPropertyChanged(ByVal propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub

#End Region

#Region "INotifyPropertyChanging Members"

        Public Event PropertyChanging As PropertyChangingEventHandler Implements INotifyPropertyChanging.PropertyChanging

        ' Used to notify that a property is about to change
        Private Sub NotifyPropertyChanging(ByVal propertyName As String)
            RaiseEvent PropertyChanging(Me, New PropertyChangingEventArgs(propertyName))
        End Sub

#End Region

    End Class

    <Table()>
    Public Class Book
        Implements INotifyPropertyChanged, INotifyPropertyChanging

        ' Define ID: private field, public property, and database column.
        Private _bookID As Integer
        <Column(IsPrimaryKey:=True, IsDbGenerated:=True, DbType:="INT NOT NULL Identity", CanBeNull:=False,
            AutoSync:=AutoSync.OnInsert)>
        Public Property BookID() As Integer
            Get
                Return _bookID
            End Get
            Set(ByVal value As Integer)
                If _bookID <> value Then
                    NotifyPropertyChanging("BookID")
                    _bookID = value
                    NotifyPropertyChanged("BookID")
                End If
            End Set
        End Property

        ' Define title: private field, public property, and database column.
        Private _title As String
        <Column()>
        Public Property Title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                If _title <> value Then
                    NotifyPropertyChanging("Title")
                    _title = value
                    NotifyPropertyChanged("Title")
                End If
            End Set
        End Property

        ' Internal column for the associated series ID value.
        <Column()>
        Friend _seriesID As Integer
        Private _series As EntityRef(Of Series)
        <Association(Storage:="_series", ThisKey:="_seriesID", OtherKey:="SeriesID")>
        Public Property BookSeries() As Series
            Get
                Return _series.Entity
            End Get
            Set(ByVal value As Series)
                NotifyPropertyChanging("BookSeries")
                _series.Entity = value
                If value IsNot Nothing Then
                    _seriesID = value.SeriesID
                End If
                NotifyPropertyChanged("BookSeries")
            End Set
        End Property

#Region "INotifyPropertyChanged Members"

        Public Event PropertyChanged As PropertyChangedEventHandler Implements NotifyPropertyChanged.PropertyChanged

        ' Used to notify that a property changed
        Private Sub NotifyPropertyChanged(ByVal propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub

#End Region

#Region "INotifyPropertyChanging Members"

        Public Event PropertyChanging As PropertyChangingEventHandler Implements INotifyPropertyChanging.PropertyChanging

        ' Used to notify that a property is about to change
        Private Sub NotifyPropertyChanging(ByVal propertyName As String)
            RaiseEvent PropertyChanging(Me, New PropertyChangingEventArgs(propertyName))
        End Sub

#End Region

单独更新字段标题,或标题和_seriesID字段工作正常。但是,当我只更改_seriesID时,则不会更新底层数据库。在这种情况下.GetModifiedMembers显示没有修改。

此处给出了对显示此问题的演示项目的引用:demo project

感谢您的关注。

1 个答案:

答案 0 :(得分:0)

_seriesID(在Book中)只是成员变量。您可以在Book之外设置它,因为它是Friend,但是没有任何反应。

另一方面,

Title是一个属性,它会触发NotifyPropertyChanged。这意味着如果您更改Book,则会通知上下文已修改Title对象。

因此,如果您更改_seriesID TitleBook对象将被标记为已修改并已保存,并且已更改_seriesID的值}。但是,如果单独更改_seriesID,则对象保持“未更改”。

我认为这是生成的代码(LINQ-to-SQL?我真的不认识它),你不应该手动修改它。如果您想要更改_seriesID,则必须设置BookSeries