2017-04-04 73 views
2

我有我的NHibernate的一到多的映射的以下問題。如果我想通過我的API方法獲取所有客戶,我收到以下錯誤:NHibernate的「無法初始化集合」

"$id":"1","Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'text/html; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"$id":"2","Message":"An error has occurred.","ExceptionMessage":"could not initialize a collection: [Designet.Models.Customer.Orders#1][SQL: SELECT orders0_.CustomerId as Custom2_2_1_, orders0_.Id as Id1_2_1_, orders0_.Id as Id1_2_0_, orders0_.CustomerId as Custom2_2_0_, orders0_.Description as Descri3_2_0_, orders0_.Price as Price4_2_0_, orders0_.Created as Create5_2_0_, orders0_.Deadline as Deadli6_2_0_ FROM Order orders0_ WHERE orders0_.CustomerId=?]","ExceptionType":"NHibernate.Exceptions.GenericADOException","StackTrace":" w NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type)\r\n

底下粘貼了我的代碼。我一直在尋找在谷歌和我的問題的答案,但沒有什麼,人們提出,幫助。我不明白哪部分代碼會拋出錯誤。也許你有一些主張,我該怎麼做才能解決問題?我很樂意爲你提供幫助。

客戶模式:

public class Customer 
    { 
     public virtual int Id { get; set; } 
     public virtual string Name { get; set; } 

     public virtual IList<Order> Orders { get; set; } 

     public Customer() 
     { 
      Orders = new List<Order>(); 
     } 
    } 

客戶映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" 
        assembly="Designet" namespace="Designet.Models"> 
    <class name="Customer" table="Customer" dynamic-update="true" lazy="true" > 
    <cache usage="read-write"/> 
    <id name="Id" column="Id" type="int"> 
     <generator class="native" /> 
    </id> 
    <property name="Name" /> 
    <bag name="Orders" lazy="true" inverse="true" > 
     <key column="CustomerId"/> 
     <one-to-many class="Designet.Models.Order"/> 
    </bag>  
    </class> 
</hibernate-mapping> 

客戶數據庫:

CREATE TABLE [dbo].[Customer] (
    [Id] INT   IDENTITY (1, 1) NOT NULL, 
    [Name] NVARCHAR (255) NULL, 
    PRIMARY KEY CLUSTERED ([Id] ASC) 
); 

訂貨型號:

public class Order 
    { 
     public virtual int Id { get; set; } 
     public virtual string Description { get; set; } 
     public virtual decimal Price { get; set; } 
     public virtual DateTime Created { get; set; } 
     public virtual DateTime Deadline { get; set; } 

     public virtual int CustomerId { get; set; } 
     public virtual Customer Customer { get; set; } 

    } 

高階映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" 
        assembly="Designet" namespace="Designet.Models" 
    <class name="Order" table="Order" dynamic-update="true" lazy="true" > 
    <cache usage="read-write"/> 
    <id name="Id" column="Id" type="int"> 
     <generator class="native" /> 
    </id> 

    <many-to-one name="Customer" column="CustomerId"/> 

    <property name="Description" /> 
    <property name="Price" /> 
    <property name="Created" /> 
    <property name="Deadline" /> 
    <property name="CustomerId" not-null="true" /> 

    </class> 
</hibernate-mapping> 

訂單數據庫:

CREATE TABLE [dbo].[Order] (
    [Id]   INT   IDENTITY (1, 1) NOT NULL, 
    [Description] NVARCHAR (MAX) NULL, 
    [Price]  SMALLMONEY  NULL, 
    [Created]  DATETIME  NOT NULL, 
    [Deadline] DATETIME  NULL, 
    [CustomerId] INT   NOT NULL, 
    PRIMARY KEY CLUSTERED ([Id] ASC), 
    FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([Id]) 
); 

回答

1

從您的評論JSON鏈接內部異常寫Incorrect syntax near the keyword 'Order'.你的表被命名爲Order,這是一個SQL關鍵字。它必須逃脫。爲此使用後傾。用於映射語義的正確性

<class name="Order" table="`Order`" ... 

注:
bag應該被映射爲ICollection<>而非IList<>。一個IList<>是映射list:一個集合,其中每個元素具有記錄在數據庫中的索引,從零到集合的大小減一,而沒有任何孔。這是不太可能的。

此外,在您的模型中,您應該使用set而不是bag:a bag允許重複。您的模型不允許它們,訂單可能只會在客戶訂單集合中出現一次。如果更改爲set,請在代碼中使用ISet<>

注意事項對您的問題
你的錯誤消息被截斷時,不允許檢查什麼是你的問題的原因。該GenericADOException應該包含另一個InnerException,但您提供的消息不顯示它。

請獲取完整的異常消息,並請嘗試以可讀的方式發佈:「漂亮打印」您的JSON,或更好的是,獲取異常.ToString()輸出。

+0

我已經改變了'bag'到'set'和'的IList <>''到的ISet <>',但它並沒有幫助。有一個完整的錯誤:http://www.jsoneditoronline.org/?id=48b540615c2550df97c1cafdb3550502 –

+0

及其包含的回答你的麻煩。回答編輯。 –

+0

現在它的工作。非常感謝你! –

相關問題