Lazy property throws casting exception "Unable to cast object of type 'System.Object' to type 'System.Byte[]" if object is beeing created and refreshed within the same session:
[Test] public void Can_LazyPropertyCastingException() { //following causes exception "Unable to cast object of type 'System.Object' to type 'System.Byte[]'. //notice that ClassA.Blob_Field s declared as lazy property //PS. Exception is beeing thrown only if object was created within the same session
using (ISession session = this.OpenSession()) { ClassA classA;
#region create and save new entity classA = new ClassA(); classA.Text = "new entity"; classA.Blob_Field = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; using (ITransaction trans = session.BeginTransaction()) { session.Save(classA); trans.Commit(); } #endregion
session.Refresh(classA);
classA.Text = "updated entity"; using (ITransaction trans = session.BeginTransaction()) { session.SaveOrUpdate(classA); trans.Commit(); }
Lazy property throws casting exception "Unable to cast object of type 'System.Object' to type 'System.Byte[]"
if object is beeing created and refreshed within the same session:
[Test]
public void Can_LazyPropertyCastingException()
{
//following causes exception "Unable to cast object of type 'System.Object' to type 'System.Byte[]'.
//notice that ClassA.Blob_Field s declared as lazy property
//PS. Exception is beeing thrown only if object was created within the same session
using (ISession session = this.OpenSession()) {
ClassA classA;
#region create and save new entity
classA = new ClassA();
classA.Text = "new entity";
classA.Blob_Field = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
using (ITransaction trans = session.BeginTransaction()) {
session.Save(classA);
trans.Commit();
}
#endregion
session.Refresh(classA);
classA.Text = "updated entity";
using (ITransaction trans = session.BeginTransaction()) {
session.SaveOrUpdate(classA);
trans.Commit();
}
session.Refresh(classA);
}
}
Check attached test.
Similar to fixed https://nhibernate.jira.com/browse/NH-2510#icft=NH-2510
https://nhibernate.jira.com/browse/NH-2510