ASP.NET WEB API处理2个相关表

问题描述 投票:0回答:1

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

首先,我使用WEB-API和Entity Framework创建一个控制器。使用我的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解决此问题?

asp.net-mvc entity-framework linq asp.net-web-api
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 
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.