2010-07-13 73 views
0

我有什麼打擊我作爲一個非常不尋常的行爲從Linq爲NHibernate。Linq for NHibernate奇怪的Count()行爲

一般來說,我所有的實體類都可以正常工作,但其中一個類在以下情況下會從NHibernate命名空間中拋出「NonUniqueResult」異常。

如果我把以下內容:

getSession<MyClass>().Linq<MyClass>().Count(); 

它拋出異常。如果我打電話

getSession<MyClass>().Linq<MyClass>().ToList().Count(); 

它沒有。

這個類的其他CRUD操作沒有問題,所以我不認爲這是我的映射。

我的猜測是它與Count()運算符最終如何物化爲SQL查詢有關,但除此之外,我不確定在哪裏尋找。

更新以包括有關

<?xml version="1.0" encoding="utf-16"?> 
<hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="My.App.MyClass, My.App" table="MyClass"> 
<id name="Id" access="property" column="Id" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000"> 
    <generator class="guid.comb"> 
    </generator> 
</id> 
<version name="TimeStamp" access="property" column="TimeStamp" type="Int32" /> 
<property name="CreatedOn" access="property" type="System.DateTime"> 
    <column name="CreatedOn"/> 
</property> 
<property name="Ticker" access="property" type="Int32"> 
    <column name="Ticker"/> 
</property> 
<property name="DifferentTicker" access="property" type="Int32"> 
    <column name="DifferentTicker"/> 
</property> 
<property name="SomeDecimal" access="property" type="System.Decimal"> 
    <column name="SomeDecimal"/> 
</property> 
<property name="StillAnotherTicker" access="property" type="Int32"> 
    <column name="StillAnotherTicker"/> 
</property> 
<property name="IsActive" access="property" type="Boolean"> 
    <column name="IsActive"/> 
</property> 
<many-to-one name="RelatedThing" access="property" class="My.App.RelatedThing, My.App" column="RelatedThingId" lazy="proxy" /> 
<many-to-one name="OtherRelatedThing" access="property" class="My.App.OtherRelatedThing, My.App" column="OtherRelatedThingId" lazy="proxy" /> 
<bag name="_schedule" access="property" table="Schedule" lazy="false" cascade="all-delete-orphan"> 
    <key column="MyClassId" /> 
    <one-to-many class="My.App.Schedule, My.App" /> 
</bag> 
<bag name="Vectors" access="property" table="Vectors" lazy="false"> 
    <key column="MyClassId" /> 
    <many-to-many class="My.App.Channels.BaseVector, My.App" column="vectorid"/> 
</bag> 
</class> 
</hibernate-mapping> 

回答

0

一個簡單的單元測試這兩個版本的工作對我罰款之類的映射。在這個例子中:

using (var tx = Session.BeginTransaction()) 
{ 
    int count = Session.Linq<Customer>().Count(); 
    Assert.Equal(2, count); 

    count = Session.Linq<Customer>().ToList().Count(); 
    Assert.Equal(2, count); 

    tx.Commit(); 
} 

第一查詢執行SQL計數:

NHibernate: SELECT count(*) as y0_ FROM "Customer" this_ 

而第二個獲得所有收益的所有項目到一個臨時目錄,然後在列表上調用COUNT():

NHibernate: SELECT this_.Id as Id9_0_, this_.Name as Name9_0_ FROM "Customer" this_ 

這使我相信您的映射可能存在問題。你真的想得到第一個工作,因爲第二個不會爲大數據集進行擴展。

+0

就性能問題達成一致。我真的不知道在地圖上可能會出現什麼情況,我已經在上面添加了上面提到的類的映射,以防您可以看到。我使用ActiveRecord來完成映射,所以這就是它生成的。再次,沒有看到其他課程中的錯誤,並且這一個適用於所有其他操作。 – 2010-07-14 03:42:50

+0

但是這個錯誤與Count()沒有任何關係,因爲它不是SQL的一部分。我會假設你得到與「getSession ().Linq ().ToList()」? – 2010-07-19 18:57:47