0
我有一個SQL Server表,其中列NameHash
與binary(16)
類型,它允許空值。如何在C#中將System.Data.Linq.Binary分配給null?
在我的C#應用程序,我有一個非常基本的MD5哈希擴展方法:
public static byte[] CalculateMD5HashBinary(this string input)
{
if (String.IsNullOrEmpty(input))
{
return null;
}
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
return md5.ComputeHash(inputBytes);
}
我使用LINQ to SQL
分配值:
var fooBar = MyContext.FooBars.First();
fooBar.NameHash = fooBar.Name.CalculateMD5HashBinary();
MyContext.SubmitChanges();
當Name
爲空或爲空, NameHash
列應該爲空。但是,當我將這個值保存在數據庫中時,我檢查了我看到的十六進制字符串0x00000000000000000000000000000000
的值。
它爲什麼這樣做?我如何獲得正確分配到NameHash
二進制列中的空值?