我有我的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])
);
我已經改變了'bag'到'set'和'的IList <>''到的ISet <>',但它並沒有幫助。有一個完整的錯誤:http://www.jsoneditoronline.org/?id=48b540615c2550df97c1cafdb3550502 –
及其包含的回答你的麻煩。回答編輯。 –
現在它的工作。非常感謝你! –