locked
rowVersion gets mapped to byte[8] in Entityframe work but when manually casting it's byte[18] RRS feed

  • Question

  • User715841376 posted

    I am getting rowVersion from database as byte[8]

    var rowVersion= new MyContext().Employee.FirstOrDefault(x => x.Id).rowVersion;
    // suppose above the actual databse values is 0x0000000000038B8C
    var rowVersionToLong = BitConverter.ToInt64(rowVersion,0);

    Now if i manually do this String rowversionStr = "0x0000000000038B8C"; byte[] mybyteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(rowversionStr);

    This gives me byte[18] and when i convert it to Int64 it gives me different value.

    i am not getting this.

    I want to pass rowVersion as parameter to WebApi get method. as Byte[] is not allowed so i am passing it string

    Wednesday, July 1, 2015 9:49 AM

Answers

  • User-84896714 posted

    Hi internetbeans,
    Thank you for your post.
    Did you convert byte[] RowVersion to string like this?

    public static string ToString(byte[] rowversionArr)
    {
        return string.Format("0x{0}", BitConverter.ToInt64(rowversionArr, 0).ToString("X16"));
    }

    If you want to convert string back to byte[], you could use code below.

    public static byte[] Parse(string rowversionStr)
    {
    return BitConverter.GetBytes(Convert.ToInt64(rowversionStr, 16)); }

    You could not use ASCIIEncoding.ASCII.GetBytes, because the string is not generate by ASCIIEncoding.ASCII.GetString method.

    internetbeans

    byte[] mybyteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(rowversionStr);

    Hoping my reply could be helpful to you.
    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 2, 2015 1:37 AM