none
Version Number Not Incrementing RRS feed

  • Question

  •  

    I want to do concurrency with an Int version number and Linq to SQL

     

    I have set the Time Stamp to True for my VersionNum column but the number is not changing on UPDATE

     

    Here is the code, sorry but it's VB.NET

     

    Table:

     

    Code Snippet

    CREATE TABLE [dbo].[Albums](

    [AlbumID] [int] NOT NULL,

    [Title] [nvarchar](100) NOT NULL,

    [ArtistID] [int] NOT NULL,

    [LabelID] [int] NULL,

    [GenreID] [int] NULL,

    [AlbumCoverID] [int] NULL,

    [Modified] [datetime] NOT NULL CONSTRAINT [DF_Albums_Modified] DEFAULT (getdate()),

    [VersionNum] [int] IDENTITY(1,1) NOT NULL,

    CONSTRAINT [PK_Albums] PRIMARY KEY CLUSTERED

     

     

    Linq to SQL col definition:

    Code Snippet

     

    <Column(Storage:="_VersionNum", AutoSync:=AutoSync.Always, DbType:="Int NOT NULL IDENTITY", IsVersion:=true)>  _

    Public Property VersionNum() As Integer

    Get

    Return Me._VersionNum

    End Get

    Set

    If ((Me._VersionNum = value) _

    = false) Then

    Me.OnVersionNumChanging(value)

    Me.SendPropertyChanging

    Me._VersionNum = value

    Me.SendPropertyChanged("VersionNum")

    Me.OnVersionNumChanged

    End If

    End Set

    End Property

     

     

    Unit Test:

     

    Code Snippet

       <TestInitialize()> Public Sub MyTestInitialize()
            _ctx1 = New TestDataBaseDataContext()
            _ctx1.Log = Console.Out

            _ctx2 = New TestDataBaseDataContext()
            _ctx2.Log = Console.Out
        End Sub


        <TestCleanup()> Public Sub MyTestCleanup()
            _ctx1.Dispose()
            _ctx2.Dispose()
        End Sub

        <TestMethod()> Public Sub TestMethod1()
            Dim anAlbum = (From a In _ctx1.Albums Where a.AlbumID = 1 Select a).Single
            Dim sameAlbum = (From a In _ctx2.Albums Where a.AlbumID = 1 Select a).Single
            Dim rnd = New Random()

            anAlbum.Title = "Audioslave" + rnd.Next.ToString

            sameAlbum.Title = "1Audioslave" + rnd.Next.ToString

            _ctx1.SubmitChanges()

            _ctx2.SubmitChanges()
        End Sub

     

     

     

    Friday, October 17, 2008 6:03 PM

Answers

  • LINQ to SQL does not update your version columns. It expects the database to do it.  You are using IDENTITY for your version column. This type is used for primary keys, not version numbers. IDENTITY is only assigned by the database when the row is inserted and not changed after that.  The data type you want to use is TIMESTAMP.

     

    Saturday, October 18, 2008 7:09 PM
    Moderator