我有一个过程,需要一个参数是一个时间戳,参数是一个SQL Server RowVersion。TimeStamp参数CreateSQLQuery nHibernate
在包含我接收为一个字节[]的RowVersion该映射表,如下所示:
public class ClassTest
{
public virtual byte[] RowVersion {get; set;}
}
public void Override(ModelMapper mapper)
{
mapper.Class<ClassTest>(m =>
{
m.Table("tblTable");
m.Version(x => x.Rowversion, x =>
{
x.Generated(VersionGeneration.Always);
x.Type(new NHibernate.Type.BinaryBlobType());
x.UnsavedValue(null);
x.Column(c =>
{
c.Name("RowVersion");
c.NotNullable(true);
c.SqlType("timestamp");
});
});
});
}
}
}
我的问题是如何为参数我的byte []发送自过程期待TimeStamp,试过这种方式,但它不起作用。
public static int TestProc(ISessionFactory factory,
byte[] rowVersion)
{
var query = session.CreateSQLQuery("exec proc_test :RowVersion")
.SetParameter("RowVersion", rowVersion, NHibernateUtil.Timestamp)
.List();
}
从数据类型到的日期时间时间戳隐式转换是不允许的。使用CONVERT函数来运行此查询。
解决方案
我解决了这个问题,也许不是最优雅的方式,但解决我的问题。我将字节数组(byte [])转换为Int64(因为我的字节数组是8个字符到4个字符,所以情况只能转换为Int32。在我的过程中,将参数TIMESTAMP的类型更改为BIGINT,既作为应用程序的数据库,为BIGINT的RowVersion转换值,并且字节[]到Int64的是一样的。
byte[] rowVersion;
var rowVersion64 = BitConverter.ToInt64(rowVersion, 0);
var query = session.CreateSQLQuery("")
.SetInt64("RowVersion", rowVersion64)
.List()
由于重复的问题说,'NHibernateUtil.Timestamp'是一个DateTime,你应该使用'Binary'或者'BinaryBlob'。这个错误已经提到'datetime'的值被传递了 –