伙计们,我与名为 user 的同一个类有一对多的关系,
我在 web apis get 方法中返回一个用户实例,只要我没有,它就可以正常工作
User
ID | Name
0 | A
1 | B
Friends Table ( This table is used to build the one to many relationship )
User_ID | Friend_ID
1 | 0
0 | 1
这是我的GetMethod
[HttpGet]
public Models.User Authenticate()
{
try
{
return db.Users.SingleOrDefault(x => x.ID == 0 ) ;
}
catch
{
return null;
}
}
如果我得到并返回一个用户实体,那么我会得到打击错误
谁能帮我解决以下错误?
{"Message":"发生错误。","ExceptionMessage":"'ObjectContent`1' 类型未能序列化内容类型 'text/html; charset=utf-8' 的响应正文。","ExceptionType ":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"发生错误。","ExceptionMessage":"检测到类型为 'System.Data.Entity.DynamicProxies 的自引用循环.Friend_E5C79F4736EB2750392FFC7061B18E9C12F15F9583409603C75C5D1B1F7358D4'。路径'Friends1[0].User.Friends'。","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":"在 Newtonsoft.Json.Serializer.JsonSerializer。CheckForCircularReference(JsonWriter writer,Object value,JsonProperty property,JsonContract contract,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 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member,JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 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 在 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 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter 编写器,对象值,JsonObjectContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member,JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n在 Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)\r\n 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()\r\n 在 System.Threading.Tasks.TaskHelpers。 RunSynchronously(Action action, CancellationToken token)"}}序列化(JsonWriter jsonWriter,对象值)\r\n 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously(Action 操作,CancellationToken 令牌)"}}序列化(JsonWriter jsonWriter,对象值)\r\n 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously(Action 操作,CancellationToken 令牌)"}}