同じテーブルに格納されている関連する多対多エンティティの簡単なクエリー可能な(およびInclude-able)クロスセクションを持つこの最終目標を達成するためのより良い方法はありますか?EF7実装TPH + M2M
私は結合テーブルにTPHを実装せずに始めましたが、そのためにはクエリの中の1つのタイプまたは別のタイプが消費されてしまいます。
// table Related: [Id]
public class Related
{
public Guid Id { get; set; }
public List<RelatedOther> RelatedOthers { get; set; } = new List<RelatedOther>();
public List<RelatedOtherOne> RelatedOtherOnes { get; set; } = new List<RelatedOtherOne>();
public List<RelatedOtherTwo> RelatedOtherTwos { get; set; } = new List<RelatedOtherTwo>();
}
// table RelatedOther: [RelatedId, OtherId, Type]
public abstract class RelatedOther
{
public Guid RelatedId { get; set; }
public Guid OtherId { get; set; }
public Related Related { get; set; }
public Other Other { get; set; }
public abstract RelatedOtherType Type { get; }
}
public class RelatedOtherOne : RelatedOther
{
public override RelatedOtherType Type => RelatedOtherType.One;
// should be unnecessary, 'Other' should be correct type
public OtherOne OtherOne { get; set; }
}
public class RelatedOtherTwo : RelatedOther
{
public override RelatedOtherType Type => RelatedOtherType.Two;
// should be unnecessary, 'Other' should be correct type
public OtherTwo OtherTwo { get; set; }
}
public enum RelatedOtherType : int
{
One = 1,
Two = 2
}
// table Other: [Id, OneProp, TwoProp]
public abstract class Other
{
public Guid Id { get; set; }
public List<RelatedOther> RelatedOthers { get; set; } = new List<RelatedOther>();
}
public class OtherOne : Other
{
public string OneProp { get; set; }
}
public class OtherTwo : Other
{
public string TwoProp { get; set; }
}
TPHは、M2Mは(HasKeyにlike this +弁別がマッピングされているlike this
マッピングされている)
これはさらに複雑になります(不可能ではない?) '関連' 実体のようなTPH戦略に進化「その他」
m2mアソシエーションの終わりがTPHの場合、それらのTPHタイプのいずれかの両端にナビゲーションプロパティを公開するかどうかにかかわらず、結合テーブル自体もそのTPHを反映する必要があります。それは単なるクリーンなデザインです。これにより、フレームワークはキャストを使って '[NotMapped]'として持っているNavigationプロパティの変換を処理できます。 – JoeBrockhaus
そして、私の基底クラスで 'abstract'を使うと、その基底型だけを照会することができません。結果セットとして2つ以上の派生型が返された場合、EFは正しい派生型をインスタンス化し、対応する派生型のインスタンスに自動的にそのインスタンスが移入されます。モデルに2つの派生型がある場合、基本クラスは抽象クラスである必要があります。 EventType識別子の値を持たないEvent2Locationのインスタンスは実際には意味がありません。したがって、決してEvent2Locationのバニラインスタンスを作成することはできません。 – JoeBrockhaus