我想创建一个WebApi使用实体框架从数据库中读取数据。使用WebAPI中的实体框架的表上的GET调用异常
我的数据模型设计是这样的:
我已经加入SportsController
名为的WebAPI控制器是这样的:
public class SportsController : ApiController
{
public HttpResponseMessage Get()
{
try
{
using (MyLeagueDBEntities dbEntity = new MyLeagueDBEntities())
{
return Request.CreateResponse(HttpStatusCode.OK, dbEntity.tbl_Sports.ToList());
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
}
上调用来自浏览器的请求,我的WebAPI控制器GET方法,我得到如下所示的异常。但在调试模式下,我可以看到它能够从数据库中获取数据而没有任何错误,但是一旦控件移出该方法,它就会抛出以下异常。
异常详细信息:
“消息”:“发生错误”。,
“ExceptionMessage”:“在‘ObjectContent`1’型未能序列化内容类型的响应人体的应用/ JSON;字符集= UTF-8' “,
”ExceptionType“:” System.InvalidOperationException”
“堆栈跟踪”:空,
“的InnerException”:{ “消息”: “发生错误”。 ,
“ExceptionMessage”:“在System.Da上从'tbl_Tournament'获取值时出错ta.Entity.DynamicProxies.tbl_Sports_7461105FEAC4FCCBDED534FCE44707AF9C7DFE1E4AD69B1E7E3777FBB953F184' “,
”ExceptionType。“:” Newtonsoft.Json.JsonSerializationException “
”堆栈跟踪“:”
在Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(目标对象)\ r \ n
在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter作家,对象的值,JsonContainerContract合同,JsonProperty构件,JsonProperty属性,JsonContract & memberContract,对象& memberValue个)\ r \ n
在Newtonsoft.Json.Serialization .JsonSerializerInternalWriter.SerializeObject(JsonWriter作家,对象值,JsonOb JSONProperty成员JsonContainerContract collectionContract JsonProperty containerProperty)\ r \ n>在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n
在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter作家,IEnumerable的值,JsonArrayContract合同,JsonProperty构件,JsonContainerContract collectionContract,JsonProperty containerProperty个)\ r \ n
在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter作家,对象值,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n
在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象的值,类型的objectType个)\ r \ n
在Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象的值,类型的objectType个)\ r \ n
at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)\ r \ n
at System.Net.Http.Formatting.JsonMediaTypeFormatter。WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)\ r \ n
at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,HttpContent content)\ r \ n
在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型,对象的值,流writeStream,HttpContent内容,transportContext transportContext,的CancellationToken的CancellationToken个)\ r \ n从先前位置堆栈跟踪的结束,其中的例外是抛出
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ r \ n
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务个)\ r \ n
在System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext() “”的InnerException“:{” 消息“:”发生了错误“,
”ExceptionMessage“:”ObjectContext实例已被处置,不能再用于需要连接的操作。“,
”ExceptionType“:”System.ObjectDisposedException“,
“StackTrace”:“
at System.Data.Entity.Core.Objects.ObjectContext.get_Connection()\ r \ n
在System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable
1 forMergeOption个)\ r \ n
在System.Data.Entity.Core.Objects.ObjectQuery1.Execute(MergeOption mergeOption)\r\n
1.Load(列表
at System.Data.Entity.Core.Objects.DataClasses.EntityCollection1 collection, MergeOption mergeOption)\r\n
1.Load(MergeOption mergeOption) \ r \ n
at System.Data.Entity.Core.Objects.DataClasses.EntityCollection
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()\ r \ n
at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty [TItem]( TItem propertyValue,String relationshipName,String targetRoleName,Boolean mustBeNull,Object wrapperObject)\ r \ n
at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior。 <> c__DisplayClass7`2.b__1(TProxy代理,TItem项个)\ r \ n
在System.Data.Entity.DynamicProxies.tbl_Sports_7461105FEAC4FCCBDED534FCE44707AF9C7DFE1E4AD69B1E7E3777FBB953F184.get_tbl_Tournament(个)\ r \ n
在Gettbl_Tournament(对象)\ r \ n
在Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象 目标)“}}}
我新的实体框架和的WebAPI,一个请求帮助我解决这个问题。
PFB快照,错误消息记录在fidd上LER。目前越来越HTTP 500内部服务器错误,
谢谢队友。正如所建议的,我使用下面的行从DB读取数据之前禁用了惰性负载,它确实解决了我的问题。 dbEntity.Configuration.LazyLoadingEnabled = false; – Manju