我使用Code First。我有一個Topic類和一個表決類。話題有票。反向代碼屬性的代碼第一個數據註釋
public class Topic
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<Vote> Votes { get; set; }
}
投票類指向主題,所以我可以在代碼中獲得給定投票的主題。
public class Vote
{
public Guid Id { get; set; }
public bool IsUp { get; set; }
public DateTime WhenVoted { get; set; }
public Topic Topic { get; set; }
}
當我查詢像var topics = context.Topics.Include("Votes");
,我得到一個堆棧溢出,但如果我從投票刪除提及主題,它加載的罰款。我相信我可以添加一個數據註釋來表明這些代表了相同的關係。如果是這樣的話,有人能指出我正確的方向嗎?
調用堆棧一再重複這似乎表明串行器試圖遞歸呈現遞歸關係。所以也許告訴Vote對象不要序列化Topic屬性在這裏是正確的答案,但是如果有數據註解的答案,我想知道它是什麼。
System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) Unknown
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) Unknown
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) Unknown
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) Unknown
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) Unknown
註釋http://msdn.microsoft.com/en-us/data/jj591583,包含堆棧跟蹤以便其他人可以幫助 –
數據庫在自動生成.cs時如何處理這種關係? – Langdon