エラーがあります。以下のコードと例外メッセージを確認できます。
ユーザークラス
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; }
}
費用収入のクラス
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; }
}
クラスのマップ
public class UserMap : ClassMap<User>
{
public UserMap() {
Table("TUsers");
Id(x => x.ID);
Map(x => x.AddedDate);
Map(x => x.PassWord);
Map(x => x.UserName);
}
}
public class expenseincomesMap : ClassMap<expenseincomes>
{
public expenseincomesMap()
{
Table("texpenseincomes");
Id(x => x.ID);
Map(x => x.Amount);
Map(x=>x.ExpIncTypeID);
References(x => x.User).Column("UserID").Cascade.All();
}
}
Web API 構成クラス
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);
[http://localhost:2711/odata/expenseincomes/get] URL を呼び出すと、この例外メッセージが表示されます。
"$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.<>c__DisplayClassd.<WriteToStreamAsync>b__c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"}}
しかし、URL => [http://localhost:2711/odata/Users/get] に問題はありません。