.net 为什么即使是最简单的NHibernate示例标准也是';不行吗?

.net 为什么即使是最简单的NHibernate示例标准也是';不行吗?,.net,nhibernate,criteria,.net,Nhibernate,Criteria,我想做最简单的工作:获得名为“土豆”的产品 解决方案2固定为: // solution 2 - Using Example Product exampleProduct = new Product(); exampleProduct.Name = "Potatoes"; return Session.CreateCriteria<Product>().Add(Example.Create(exampleProduct).ExcludeZeroes()).List<Product

我想做最简单的工作:获得名为“土豆”的产品

解决方案2固定为:

// solution 2 - Using Example
Product exampleProduct = new Product();
exampleProduct.Name = "Potatoes";
return Session.CreateCriteria<Product>().Add(Example.Create(exampleProduct).ExcludeZeroes()).List<Product>();
//解决方案2-使用示例
Product exampleProduct=新产品();
示例product.Name=“土豆”;
return Session.CreateCriteria().Add(Example.Create(exampleProduct).ExcludeZeroes()).List();

它们不一定相等。举例来说,您的查询可能包括一些具有默认值的其他字段(可能类似于
Active=false

您应该排除不希望用于筛选器的字段。一个好的开始是
ExcludeZeroes()

在任何情况下,都要检查结果SQL。您可以使用NHibernate配置文件中的以下属性执行此操作:

<property name="show_sql">true</property>
true

…或者您可以使用NHProf之类的工具,或者您的DB profiler工具、log4net输出等。

非常感谢。我更新了问题以显示解决方案。
SELECT this_.Id as Id0_0_, this_.Name as Name0_0_, this_.Price as Price0_0_ FROM [Product] this_ WHERE (this_.Name = 'Potatoes' and this_.Price = 0); 
// solution 2 - Using Example
Product exampleProduct = new Product();
exampleProduct.Name = "Potatoes";
return Session.CreateCriteria<Product>().Add(Example.Create(exampleProduct).ExcludeZeroes()).List<Product>();
<property name="show_sql">true</property>