我有設置了一個多到許多關係的兩個實體:實施IEntityWithRelationships導致相關對象停止加載
類別:
public class Category : IEntityWithRelationships
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public virtual ICollection<User> Users { get; set; }
//Json.NET needs this in order to serialize the object
private RelationshipManager rm;
RelationshipManager IEntityWithRelationships.RelationshipManager
{
get
{
if (rm == null)
rm = RelationshipManager.Create(this);
return rm;
}
}
}
和用戶:
public class User
{
public int Id { get; set; }
public ICollection<Category> Categories { get; set; }
}
相關對象加載正常,但我需要將類別對象序列化爲Json和Json.Net不斷拋出:
無法序列化RelationshipManager對象。當RelationshipManager屬於未實現IEntityWithRelationships的 實體對象時,無法序列化此類型的 對象。
所以我實現了IEntityWithRelationships
接口,但現在我的相關對象沒有加載。
我試過一個自定義ContractResolver
,但我仍然得到上述異常。我怎樣才能讓我的相關對象加載?