それらの間に関係を持ついくつかの POCO エンティティを作成しました。たとえば、「個人」エンティティには、「コレクション」エンティティとの OneToMany 関係があります。これが私がそれらを定義した方法です:
[DataContract(IsReference=true)]
[KnownType(typeof(User))]
[KnownType(typeof(Address))]
[KnownType(typeof(Collection))]
[KnownType(typeof(Donation))]
[KnownType(typeof(CollectorRating))]
[KnownType(typeof(DonatorRating))]
[Table("Individuals")]
public class Individual : User
{
// ... Lots of attributes
[DataMember]
[InverseProperty("Collector")]
public virtual ICollection<Collection> Collections { get; set; }
// Lots of other attributes
}
そして Collection エンティティ:
[DataContract(IsReference=true)]
[KnownType(typeof(Individual))]
[KnownType(typeof(Organization))]
[KnownType(typeof(Donation))]
[KnownType(typeof(DeliveryDay))]
[KnownType(typeof(Address))]
[Table("Collections")]
public class Collection
{
// Other attributes
[DataMember]
[InverseProperty("Collections")]
public virtual Individual Collector { get; set; }
// ... Attributes
}
私のサービスは Silverlight 互換サービスで、次のように定義されています。
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class IndividualService
{
[OperationContract]
public User GetByEmail(string email)
{
using (var context = new EntitiesContext())
{
Individual user = context.Individuals.SingleOrDefault(u => u.Email == email);
return user;
}
}
}
これは期待どおりに機能します。データ メンバーが設定された個々のオブジェクトと、コレクションの null 配列を受け取ります。
ただし、関係を含めようとすると:
context.Individuals.Include("Collections").SingleOrDefault(u => u.Email == email)
スタック オーバーフローの例外がありますが、これは非常に厄介です。これは循環参照エラーであると確信していますが、すべての解決策を試しました (IsReference=true を DataContract 属性に追加します...)。この唯一の機能は、Collection エンティティの DataMember 属性を IgnoreDataMember 属性に置き換えることですが、双方向の関係が失われます。これは、この特定のエンティティに必要なものです...