代码之家  ›  专栏  ›  技术社区  ›  Dave Weaver

如何修改只读LINQ属性

  •  1
  • Dave Weaver  · 技术社区  · 17 年前

    我有一个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
    

    但是对用户的调用。_totallogins+=1没有被写入数据库?对如何让Linq看到我的变化有什么想法吗?

    3 回复  |  直到 17 年前
        1
  •  2
  •   DamienG    17 年前

    Public ReadOnly Property TotalLogins() As System.Nullable(Of Integer)
        Get
            Return Me.InternalTotalLogins
        End Get
    End Property
    
        2
  •  0
  •   community wiki chrissie1    17 年前
    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
    

        3
  •  0
  •   Andrew Davey    16 年前