我在使用NHibernate One-to-many mappings时遇到了问题。如果我想通过API方法获取所有Customers,我会收到以下错误信息:
"$id":"1","Message":"发生错误。", "ExceptionMessage": "ObjectContent`1类型无法序列化内容类型为'text / html; charset = utf-8'的响应正文。", "ExceptionType": "System.InvalidOperationException", "StackTrace":null,"InnerException":{"$id":"2","Message":"出现错误。", "ExceptionMessage":"无法初始化集合:[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"}}
下面是我的代码。我在Google和SO上寻找解决方案,但是没有人提出的任何建议有所帮助。我不明白哪一部分代码引发了错误。也许您有一些建议,可以解决这个问题?非常感谢您的帮助。
客户模型:
"$id":"1","Message":"发生错误。", "ExceptionMessage": "ObjectContent`1类型无法序列化内容类型为'text / html; charset = utf-8'的响应正文。", "ExceptionType": "System.InvalidOperationException", "StackTrace":null,"InnerException":{"$id":"2","Message":"出现错误。", "ExceptionMessage":"无法初始化集合:[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"}}
下面是我的代码。我在Google和SO上寻找解决方案,但是没有人提出的任何建议有所帮助。我不明白哪一部分代码引发了错误。也许您有一些建议,可以解决这个问题?非常感谢您的帮助。
客户模型:
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])
);