我有一个带有儿童收藏价格的类产品:
public class Product
{
private ICollection<Price> prices;
protected Product()
{
prices = new List<Price>();
}
}NHibernate映射如下所示
<xml version="1.0" encoding="utf-8">
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Product" table="Product">
<id name="id" column="ProductId" access="field">
<generator class="identity"/>
</id>
<bag name="prices" access="field" cascade="all-delete-orphan" lazy="true">
<key column="ProductId"/>
<one-to-many class="Product.Price"/>
</bag>
</class>
您可以看到prices-集合是延迟加载的。
从数据库中加载产品的方式如下:
public ICollection<Product> ListProducts()
{
ISession session = GetCurrentSession();
return session
.CreateCriteria(typeof(Product))
.List<Product>();
}该函数在GetCurrentSession()上引用,其内容如下:
protected ISession GetCurrentSession()
{
return sessionProvider.GetCurrentSessionFrom(sessionFactoryConfigName);
}当我加载产品时,我希望产品中Price-Collections的位置是一个代理,因为价格具有lazy-loading = true。但是在调试时,我可以使用Visual Studio watch工具查看产品,并可以看到完整内容的价格集合(价格及其所有属性)。为何会这样呢?
发布于 2011-01-07 21:02:03
由于您在调试时访问了Price属性,因此代理将加载products集合...这意味着,您已经通过“查看”价格属性触发了延迟加载过程。
您可以通过配置nhibernate来查看发生了什么,以便输出执行的SQL语句(show_sql配置选项)。
发布于 2011-01-07 21:04:13
因为VS总是触发价格的getter方法,该方法在价格为空的情况下加载所有的价格。如果您使用的是SQL Server,并且希望检查延迟加载是否按预期工作,请使用SQL Server事件探查器(如果您使用的是Express edition,请使用AnjLab SqlProfiler )。
https://stackoverflow.com/questions/4625664
复制相似问题