次のマッピングを実行する方法はありますか (データベース優先のアプローチを使用) :
テーブル: (読みやすさのみを目的として、C# に似た構文でテーブルを定義しています)
table MainItems
{
column PK not-null unique int MainItemKey;
column string Name;
column string AspectAInfo;
column string AspectBInfo;
// 0 for A, 1 for B, 2 for both (Could be replaced with 2 boolean columns)
column not-null int AspectABOrBoth;
}
table AspectAMoreInfo
{
column PK not-null unique in AspectAMoreInfoKey;
column FK not-null int MainItemKey;
column string PayLoadA;
}
table AspectBMoreInfo
{
column PK not-null unique in AspectBMoreInfoKey;
column FK not-null int MainItemKey;
column double PayLoadB;
}
エンティティ:
// Map to MainItems table if column AspectABOrBoth is 0 or 2
class TypeAItem
{
// Map to MainItemKey column
int TypeAItemKey { get; set; }
string Name { get; set; } // Map to Name column
// Navigation property to AspectAMoreInfo rows
List<TypeAMoreInfo> MoreInfo { get; set; }
// Navigation property to MainItems row when AspectABOrBoth is 2
TypeBItem OptionalInnerItemB { get; set; }
}
// Map to MainItems table if column AspectABOrBoth is 1 or 2
class TypeBItem
{
// Map to MainItemKey column
int TypeBItemKey { get; set; }
string Name { get; set; } // Map to Name column
// Navigation property to AspectBMoreInfo rows
List<TypeBMoreInfo> MoreInfo { get; set; }
}
// Map to AspectAMoreInfo table
class TypeAMoreInfo
{
// Map to AspectAMoreInfoKey column
int TypeAMoreInfoKey { get; set; }
// Navigation property to MainItems row when MainItems.AspectABOrBoth is 0 or 2
TypeAItem Owner { get; set; }
}
// Map to AspectBMoreInfo table
class TypeBMoreInfo
{
// Map to AspectBMoreInfoKey column
int TypeBMoreInfoKey { get; set; }
// Navigation property to MainItems row when MainItems.AspectABOrBoth is 1 or 2
TypeBItem Owner { get; set; }
}
私が検討した可能性のある方向性は次のとおりです。
MainItems テーブルの上に 2 つのビューを定義し、エンティティをそれらにマッピングします。
(Table-Per-Concrete-Type と一緒に、ベース タイプを使用できます。)AspectABOrBoth 列の代わりに (同じ行に) 自分自身を指す 2 つの null 許容 FK 列を MainItems テーブルに追加します
(MainItem が AspectA の場合は 1 つ、MainItem が AspectB の場合は非 null です)
(これでテーブル分割を使用できます)。 、新しい FK 列に基づいています。)