我正在使用AngularJS,EF和WebAPI。我在ObjectA和ObjectB之間有一對多的關係。Circular Reference - AngularJS/EF/WebAPI
在UI,我想遍歷對象A的列表,這樣做:
<table>
<tr ng-repeat="objectA in objectAs">
<td>objectA.objectB.Description</td>
<td>objectA.someValue</td>
</tr>
</table>
的問題是,如果我在接入層,我做的:
db.ObjectA.Include(o => o.ObjectB).ToList()
我得到一個不錯的錯誤:
Object graph for type 'ObjectB' contains cycles and cannot be serialized if reference tracking is disabled.
行,沒問題,我只需要添加:
[DataContract(IsReference = true)]
生成合約(來自EF)的* .tt文件。
的WebAPI返回有效值(沒有錯誤),但它看起來像Angluar無法處理 「引用」 回來了,它看起來像:
[ { 「的$ id」: 「1」, 「someValue中」: 「池」, 「對象B」:{ 「$ ID」: 「2」, 「描述」: 「標準」, 「ObjectAs」:[ { 「$ REF」:「1 「 }, { 」$ id「:」3「, 」someValue「:」Poolhouse「, 「對象B」:{ 「$ REF」: 「2」 }, }, }, { 「$ REF」: 「3」 }, { 「$ REF」: 「4」 }, { 「$ REF」: 「5」 }, { 「$ REF」: 「6」 }, { 「$ REF」: 「7」 }, { 「$ REF 「:」8「 }, { 」$ ref「:」9「 }, { 「$ REF」: 「10」 },{ 「$ REF」: 「11」 } ]
現在我真的不能修改我的DTO的刪除數據成員的某些屬性導航屬性。
有關最佳實踐的任何建議?我應該只是返回一個輕的DTO(只是對象A),然後有一個javascript方法查找對象B?
編輯:
我看了一下從角輸出。事實證明,它將WebAPI放入的$ ref道具轉換爲空元素{}。就像是...
[
{
"$id": "1",
"someValue": "Pool",
"objectB": {
"$id": "2",
"Description": "Standard",
"ObjectAs": [
{
},
{
"$id": "3",
"someValue": "Poolhouse",
"ObjectB": {
},
},
},
{}, {},{}, {},{}, {},{}, {}
]
所以從這裏,我看到幾個選項:
更改代碼,只返回一個singluar對象,使九月調用來獲得查找字段
設置密碼爲空出循環參考:
ObjectA.objectB.ObjectAs = null;
- 寫我自己的格式來處理輸出
我本來期望這是一個常見的問題,但似乎無法找到任何關於它的帖子。有其他人遇到這個問題嗎?
你是什麼意思角不能處理呢?你看到了什麼樣的錯誤或行爲? – HackedByChinese
Angular沒有錯誤,它只顯示一個空值... – user472292
你嘗試過使用db.Configuration.ProxyCreationEnabled = false; –