title: Nhibernate "could not initialize a collection"
body: <p>I have a following problem with my NHibernate One-to-many mappings. If I want to get all Customers, by my API methods, I get a following error:</p>
  
  <blockquote>
    <p>"$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</p>
  </blockquote>
  
  <p>Underneath I pasted my codes. I was looking for answer for my problem on google and SO, but nothing, what people proposed, helped. I don't understand which part of code throw an error. Maybe you have some proposition, what should I do, to solve the problem? I will very glad for your help.</p>
  
  <p>Customer model:</p>
  
  <pre><code>public class Customer
      {
          public virtual int Id { get; set; }
          public virtual string Name { get; set; }
  
          public virtual IList&lt;Order&gt; Orders { get; set; }
  
          public Customer()
          {
              Orders = new List&lt;Order&gt;();
          }
      }
  </code></pre>
  
  <p>Customer Mappings:</p>
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"
                     assembly="Designet" namespace="Designet.Models"&gt;
    &lt;class name="Customer" table="Customer" dynamic-update="true" lazy="true" &gt;
      &lt;cache usage="read-write"/&gt;
      &lt;id name="Id" column="Id" type="int"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="Name" /&gt;
      &lt;bag name="Orders" lazy="true" inverse="true" &gt;
        &lt;key column="CustomerId"/&gt;
        &lt;one-to-many class="Designet.Models.Order"/&gt;
      &lt;/bag&gt;    
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Customer Database:</p>
  
  <pre><code>CREATE TABLE [dbo].[Customer] (
      [Id]   INT            IDENTITY (1, 1) NOT NULL,
      [Name] NVARCHAR (255) NULL,
      PRIMARY KEY CLUSTERED ([Id] ASC)
  );
  </code></pre>
  
  <p>Order Model:</p>
  
  <pre><code> 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; }
  
      }
  </code></pre>
  
  <p>Order Mappings:</p>
  
  <pre><code>    &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"
                     assembly="Designet" namespace="Designet.Models"
      &lt;class name="Order" table="Order" dynamic-update="true" lazy="true" &gt; 
      &lt;cache usage="read-write"/&gt;
      &lt;id name="Id" column="Id" type="int"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
  
      &lt;many-to-one name="Customer" column="CustomerId"/&gt;
  
      &lt;property name="Description" /&gt;
      &lt;property name="Price" /&gt;
      &lt;property name="Created" /&gt;
      &lt;property name="Deadline" /&gt;
      &lt;property name="CustomerId" not-null="true" /&gt;
  
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Order Database:</p>
  
  <pre><code>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])
  );
  </code></pre>