Nhibernate 延迟加载
关于Nhibernate 延迟加载必须处在同一个连接,即同一个Session会话。
1.var policyId = this.policyGroupingEntityRepository.FindSingleByExpression(p => p.PolciyGroup.PolicyId == entity.PolicyId).PolciyGroup.PolicyId;
2.var policy= this.policyGroupingEntityRepository.FindSingleByExpression(p => p.PolciyGroup.PolicyId == entity.PolicyId);
new LogicControlContentEntity{
DefaultPolciyId=policy.PolciyGroup.PolicyId;
}
3.查询一个Entity完成之后,中间不加其他entity的查询访问第一个entity的属性是可以的。因为此时还处于第一个entity的连接,如果直接加着其他entity的连接。之后有访问第一个entity的属性。是不可以的因为此时连接已更换成第二个entity的连接。。
eg 1.var policy= this.policyGroupingEntityRepository.FindSingleByExpression(p => p.PolciyGroup.PolicyId == entity.PolicyId);
var policyId=policy.policyId
eg2.var policy= this.policyGroupingEntityRepository.FindSingleByExpression(p => p.PolciyGroup.PolicyId == entity.PolicyId);
var polciyList =
(from p in this.policiesEntityRepository.EntityQueryable where polciyIdList.Contains(p.Id) select p).ToList();
var policyId=policy.policyId
eg2是不可以的。
但是设置isWeb="true" 是可以的,因为此时请求是整个应用程序集的。
<!--NHibernate Facility-->
<facility id="nhibernatefacility" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration" isWeb="true">
<factory id="AOTTGateWayFactory">
<settings>
<item key="connection.provider">NHibernate.Connection.DriverConnectionProvider</item>
<item key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
<item key="connection.connection_string_name">connectionStrings</item>
<item key="dialect">NHibernate.Dialect.MsSql2008Dialect</item>
<item key="show_sql">true</item>
<item key="command_timeout">120</item>
<item key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</item>
<item key="current_session_context_class">web</item>
<!--<item key="cache.provider_class">AOTT.GateWay.DatabaseDao.Caches.MongoDbCacheProvider, AOTT.GateWay.DatabaseDao</item>
<item key="cache.region_prefix" value="mongo.cache"/>
<item key="cache.use_second_level_cache">true</item>
<item key="cache.use_query_cache">true</item>
<item key="cache.use_minimal_puts">false</item>
<item key="mongodb.connection_string_name">MongoConnectionStrings</item>
<item key="mongodb.pattern">^AOTT\.GateWay\.Model\.Repository\..+?</item>
<item key="mongodb.region_prefix">AOTT</item>-->
</settings>
<assemblies>
<assembly>AOTT.GateWay.Model</assembly>
</assemblies>
</factory>
</facility>
4. 在一个linq 查询的from 子句中想查询policy表中的policyId.可以先将policiesEntity查询出来。如下所示。
不要再from子句中给PolciyId赋值时在去访问数据库。在大数据量时容易引发异常。此外在判断一个List是否为空时可以使用any.
var polciyList =
(from p in this.policiesEntityRepository.EntityQueryable where polciyIdList.Contains(p.Id) select p).ToList();
var userEntitlementItemList = (from userId in userIdlist
from controlContent in controlContentList
select
new UserEntitlementItemEntity{ PolciyId =
!string.IsNullOrEmpty(entity.PolicyId)
? entity.PolicyId
: (polciyList.FirstOrDefault(
q => q.Id == controlContent.DefaultPolciyId)
?? defaultPolciy).PolicyId,
}).ToList();
如上所示第二种取法是错误的因为现在session已经关闭了。
5.使用using可以去掉延迟加载。
public void Create(params TEntity[] entityList)
{
if (entityList == null || entityList.All(p => p == null))
{
return;
}
using (var session = this.OpenSession())
{
session.Clear();
foreach (var entity in entityList.Where(entity => entity != null))
{
session.Save(entity);
}
this.SaveSession(session);
}
}