title: Fluent Nhibernate and Web api error
body: <p>I have an error.  You can see my codes and exception message below.</p>
  
  <p>User Class </p>
  
  <pre><code>public class User 
      {
          public virtual int ID { get; set; }
          public virtual string UserName { get; set; }
          public virtual string PassWord { get; set; }
          public virtual DateTime AddedDate { get; set; }          
          public virtual UserGroup UserGroup { get; set; }
  
      }
  </code></pre>
  
  <p>Class for expenseincomes</p>
  
  <pre><code>public class expenseincomes
      {
          public virtual int ID { get; set; }
          public virtual int ExpIncTypeID { get; set; }
          public virtual decimal Amount { get; set; }
          public virtual User User { get; set; }
  
      }
  </code></pre>
  
  <p>Map Classes</p>
  
  <pre><code>public class UserMap : ClassMap&lt;User&gt;
      {
         public UserMap() {
             Table("TUsers");
             Id(x =&gt; x.ID);
             Map(x =&gt; x.AddedDate);              
             Map(x =&gt; x.PassWord);
             Map(x =&gt; x.UserName);
  
         }
  
      }
  
    public class expenseincomesMap : ClassMap&lt;expenseincomes&gt;
      {
          public expenseincomesMap()
          {
             Table("texpenseincomes");
             Id(x =&gt; x.ID);
             Map(x =&gt; x.Amount);
             Map(x=&gt;x.ExpIncTypeID);
             References(x =&gt; x.User).Column("UserID").Cascade.All();
         }
      }
  </code></pre>
  
  <p>Web Api Config Class</p>
  
  <pre><code>config.EnableQuerySupport();
                      config.Routes.MapHttpRoute(
                      name: "ActionApi",
                      routeTemplate: "odata/{controller}/{action}/{id}",
                      defaults: new { id = RouteParameter.Optional }
                      );
  
                      var json = config.Formatters.JsonFormatter;
                      json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
                      config.Formatters.Remove(config.Formatters.XmlFormatter);
  </code></pre>
  
  <p>when i call [http://localhost:2711/odata/expenseincomes/get] url   I get this exception message .   </p>
  
  <pre><code>"$id":"1","Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"$id":"2","Message":"An error has occurred.","ExceptionMessage":"Cannot serialize a Session while connected","ExceptionType":"System.InvalidOperationException","StackTrace":"   at NHibernate.Impl.SessionImpl.System.Runtime.Serialization.ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)\r\n   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)\r\n   at System.Net.Http.Formatting.JsonMediaTypeFormatter.&lt;&gt;c__DisplayClassd.&lt;WriteToStreamAsync&gt;b__c()\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"}}
  </code></pre>
  
  <p><strong>But there is no problem with URL  =></strong> [http://localhost:2711/odata/Users/get]</p>