0

我目前正在使用 ASP.NET MVC WEB-API。

首先,我使用 WEB-API 和实体框架创建了一个控制器。使用 my 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_Entrywith解决此问题tbl_EntryType

4

1 回答 1

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 
        });
    }
于 2013-10-21T12:17:04.587 回答