如果你抢NHib源代码,有在测试项目中的一类,这将有助于你与你所需要的:NHibernate.Test.VersionTest.Db.MsSQL.BinaryTimestamp。基本上,你必须给它一个它可以用来转换值的自定义类型。默认情况下,NHib预计该值是一个整数(请参阅第012页的第5.1.7条)。如果使用int/bigint作为版本列,则不需要自定义类型。
自定义类(从NHib源代码解除):
public class BinaryTimestamp : IUserVersionType
{
#region IUserVersionType Members
public object Next(object current, ISessionImplementor session)
{
return current;
}
public object Seed(ISessionImplementor session)
{
return new byte[8];
}
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object DeepCopy(object value)
{
return value;
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public bool IsMutable
{
get { return false; }
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
return rs.GetValue(rs.GetOrdinal(names[0]));
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
NHibernateUtil.Binary.NullSafeSet(cmd, value, index);
}
public object Replace(object original, object target, object owner)
{
return original;
}
public System.Type ReturnedType
{
get { return typeof(byte[]); }
}
public SqlType[] SqlTypes
{
get { return new[] { new SqlType(DbType.Binary, 8) }; }
}
public int Compare(object x, object y)
{
var xbytes = (byte[])x;
var ybytes = (byte[])y;
return CompareValues(xbytes, ybytes);
}
bool IUserType.Equals(object x, object y)
{
return (x == y);
}
#endregion
private static int CompareValues(byte[] x, byte[] y)
{
if (x.Length < y.Length)
{
return -1;
}
if (x.Length > y.Length)
{
return 1;
}
for (int i = 0; i < x.Length; i++)
{
if (x[i] < y[i])
{
return -1;
}
if (x[i] > y[i])
{
return 1;
}
}
return 0;
}
public static bool Equals(byte[] x, byte[] y)
{
return CompareValues(x, y) == 0;
}
}
使用类A样本映射:
public class Car
{
public virtual long CarId { get; set; }
public virtual string Name { get; set; }
public virtual byte[] LastModified { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Name: {1}, Last Modified: {2}", CarId, Name, LastModified);
}
}
public class CarMap : ClassMapping<Car>
{
public CarMap()
{
Table("Cars");
Id(car => car.CarId, mapper => mapper.Generator(Generators.Identity));
Property(car => car.Name);
Version(car => car.LastModified, mapper =>
{
mapper.Generated(VersionGeneration.Always);
mapper.Type<BinaryTimestamp>();
});
}
}
感谢您的回答,不幸的是我没有使用Fluent Nhibernate。我只是使用了3.2中新增的内置代码映射。内置的代码映射没有CustomSqlType – wllmsaccnt