How to handle Rowversion in C# class?

Dec 12, 2016 at 5:29 AM
Hello,
I have a table with a RowVersion column, I am not sure if I will use this to check for concurrency problems if using this TableDependency, but I may. What property do I give to this type so I don't get an exception "Incorrect syntax near the keyword 'END'." in "tableDependency.Start();".

I have tried using string and byte[]

Thanks
Francisco
Dec 29, 2016 at 8:27 PM
Hi. This is a bug.

I will fix as soon as possible.

Thanks for your comment.
Dec 30, 2016 at 9:01 AM
Fixed with versione 4.8.4

Thanks for your help !!!
namespace TableDependency.IntegrationTest.TypeChecks.SqlServer
{
    public class RowVersioneModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public byte[] Version { get; set; }

}

    [TestClass]
    public class RowVersionType
    {
        private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["SqlServerConnectionString"].ConnectionString;
        private static string TableName = "RowVersionTest";
        private byte[] RowVersionInsert = null;
        private byte[] RowVersionUpdate = null;

        [ClassInitialize()]
        public static void ClassInitialize(TestContext testContext)
        {
            using (var sqlConnection = new SqlConnection(ConnectionString))
            {
                sqlConnection.Open();
                using (var sqlCommand = sqlConnection.CreateCommand())
                {
                    sqlCommand.CommandText = $"IF OBJECT_ID('{TableName}', 'U') IS NOT NULL DROP TABLE [{TableName}];";
                    sqlCommand.ExecuteNonQuery();

                    sqlCommand.CommandText = $"CREATE TABLE {TableName}(Id INT, Name VARCHAR(50), Version ROWVERSION);";
                    sqlCommand.ExecuteNonQuery();
                }
            }
        }

        [TestInitialize()]
        public void TestInitialize()
        {
        }
        
        [ClassCleanup()]
        public static void ClassCleanup()
        {
            using (var sqlConnection = new SqlConnection(ConnectionString))
            {
                sqlConnection.Open();
                using (var sqlCommand = sqlConnection.CreateCommand())
                {
                    sqlCommand.CommandText = $"IF OBJECT_ID('{TableName}', 'U') IS NOT NULL DROP TABLE [{TableName}];";
                    sqlCommand.ExecuteNonQuery();
                }
            }
        }

        [TestCategory("SqlServer")]
        [TestMethod]
        public void Test()
        {
            SqlTableDependency<RowVersioneModel> tableDependency = null;

            try
            {
                tableDependency = new SqlTableDependency<RowVersioneModel>(ConnectionString, TableName);
                tableDependency.OnChanged += this.TableDependency_Changed;
                tableDependency.Start();

                Thread.Sleep(5000);

                var t = new Task(ModifyTableContent);
                t.Start();
                t.Wait(20000);
            }
            finally
            {
                tableDependency?.Dispose();
            }

            Assert.AreNotEqual(RowVersionInsert, RowVersionUpdate);
        }

        private void TableDependency_Changed(object sender, RecordChangedEventArgs<RowVersioneModel> e)
        {

            switch (e.ChangeType)
            {
                case ChangeType.Insert:
                    RowVersionInsert = e.Entity.Version;
                    break;

                case ChangeType.Update:
                    RowVersionUpdate = e.Entity.Version;
                    break;
            }
        }

        private static void ModifyTableContent()
        {
            using (var sqlConnection = new SqlConnection(ConnectionString))
            {
                sqlConnection.Open();
                using (var sqlCommand = sqlConnection.CreateCommand())
                {
                    sqlCommand.CommandText = $"INSERT INTO [{TableName}] ([Id], [Name]) VALUES (1, 'AA')";                   
                    sqlCommand.ExecuteNonQuery();
                    Thread.Sleep(1000);
                }

                using (var sqlCommand = sqlConnection.CreateCommand())
                {
                    sqlCommand.CommandText = $"UPDATE [{TableName}] SET [Name] = 'BB' WHERE [Id] = 1";
                    sqlCommand.ExecuteNonQuery();
                }
            }
        }
    }
}