2013-04-28 76 views
0

我目前正在使用ASP.NET MVC WEB-API。ASP.NET WEB API处理2相关表格

首先我使用WEB-API和实体框架创建一个控制器。 使用我的tbl_User,通过.../api/User =>调用它,它工作得很好。 (无外资键存在)

做同样的我tbl_Entry(包括tbl_EntryType),我收到以下错误:

<Error> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'. 
</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace/> 
<InnerException> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
Type 'System.Data.Entity.DynamicProxies.tbl_Entry_EAF4A2D5587BA15D1CE736067702C5D158ADFB6D6C49D43B66F64E14A4EBE8AC' with data contract name 'tbl_Entry_EAF4A2D5587BA15D1CE736067702C5D158ADFB6D6C49D43B66F64E14A4EBE8AC:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. 
</ExceptionMessage> 
<ExceptionType> 
System.Runtime.Serialization.SerializationException 
</ExceptionType> 
<StackTrace> 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOftbl_EntryToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.<>c__DisplayClass7.<WriteToStreamAsync>b__6() at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token) 
</StackTrace> 
</InnerException> 
</Error> 

如何解决使用tbl_Entry就此问题与外国包括tbl_EntryType键?

+0

是否tbl_Entry实体有一个主键? – 2013-04-28 11:21:37

回答

0

它是因为你的表是相互引用的,所以它本质上是在尝试序列化时创建一个永无止境的循环。根据您的数据的复杂性,我总是建议使用ViewModels。它迫使你只公开你想发送的列,并用序列化来解决问题。如果您不打算重复使用视图模型,只需返回一个包含所需数据的新对象。

我不知道你的应用程序的结构,但你可以做这样的事情

public object GetDealership(int id) 
    { 
     return Db.Dealerships.Find(id).Select(x => new { 
      x.SomeProperty, 
      x.RelationshipObject.SomeProperty 
     }); 
    } 

或者使用的ViewModels(推荐Automapper),但纯朴的缘故

public ViewModel GetDealership(int id) 
    { 
     return Db.Dealerships.Find(id).Select(x => new ViewModel { 
      x.SomeProperty, 
      x.RelationshipObject.SomeProperty 
     }); 
    }