【发布时间】:2014-04-08 10:42:54
【问题描述】:
我有这个代码:
lock (m_session)
{
var _result = m_session.Query<StaticContainerStorage>().Where(c => c.StorageId == storageName && c.ContainerId == null).FirstOrDefault();
if (!String.IsNullOrEmpty(_result.ContainerId))
throw new Exception();
if (_result == null)
{
_result = new StaticContainerStorage(storageName, 0);
AddContainer(_result);
}
return _result;
}
导致此查询:
select TOP (1) mfccontain0_.ID as ID0_,
mfccontain0_.StorageId as StorageId0_,
mfccontain0_.StorageIndex as StorageI3_0_,
mfccontain0_.ContainerId as Containe4_0_
from dbo.[MfcContainerStorage] mfccontain0_
where mfccontain0_.StorageId=@p0 and (mfccontain0_.ContainerId is null)
返回正确的行,但是,返回的对象将其属性ContainerId设置为一个值,该值不为null,因此抛出异常。这里会发生什么?我有多个线程访问该方法,这就是它被锁定到(单个)会话的原因。
有什么想法吗?
编辑
在我在查询之前添加m_session.Flush() 后,问题似乎消失了。仍然不知道出了什么问题。
【问题讨论】:
-
ContainerId有什么值?它与数据库中的行对应吗?会不会是某个地方设置的默认值?
-
数据库中的值为空,而对象的属性是一个真实的字符串“Kiste1”。
标签: c# nhibernate linq-to-nhibernate