1

我有一个带有只读属性的 LINQ to SQL 生成类:

<Column(Name:="totalLogins", Storage:="_TotalLogins", DbType:="Int", UpdateCheck:=UpdateCheck.Never)>  _
Public ReadOnly Property TotalLogins() As System.Nullable(Of Integer)
    Get
        Return Me._TotalLogins
    End Get
End Property

这可以防止它在外部被更改,但我想从我的类中更新属性,如下所示:

Partial Public Class User

...

    Public Shared Sub Login(Username, Password)
        ValidateCredentials(UserName, Password)

        Dim dc As New MyDataContext()
        Dim user As User = (from u in dc.Users select u where u.UserName = Username)).FirstOrDefault()
        user._TotalLogins += 1
        dc.SubmitChanges()
    End Sub

...

End Class

但是对 user._TotalLogins += 1 的调用没有被写入数据库?关于如何让 LINQ 查看我的更改的任何想法?

4

3 回答 3

2

将现有 TotalLogins 属性设置为私有或受保护,并删除只读属性。您可能还想重命名它,例如 InternalTotalLogins。

然后在部分类中手动创建一个新属性,将其公开为只读属性:

Public ReadOnly Property TotalLogins() As System.Nullable(Of Integer)
    Get
        Return Me.InternalTotalLogins
    End Get
End Property
于 2008-09-18T19:42:40.613 回答
0
Make a second property that is protected or internal(?) 

<Column(Name:="totalLogins", Storage:="_TotalLogins", DbType:="Int", UpdateCheck:=UpdateCheck.Never)>  _
protected Property TotalLogins2() As System.Nullable(Of Integer)
    Get
        Return Me._TotalLogins
    End Get
    Set(byval value as System.Nullable(Of Integer))
        Return Me._TotalLogins
    End Get
End Property

然后更新它。我认为默认情况下它不会保存只读属性。以及为什么要这样做。我现在这是一个黑客,但嘿,这就是生活。

于 2008-09-18T19:31:52.950 回答
0

在更改字段之前调用 SendPropertyChanging(),然后在调用 SendPropertyChanged("") 之后。这将使 Table 实体跟踪知道发生了什么变化。

于 2009-06-07T10:31:20.103 回答