0
多個實體是否有辦法瓶坯以下映射(使用數據庫第一種方法):實體框架 - 拆分表格具有重疊條件
表:(使用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; }
}
個
我也考慮過,但不希望採取可能的方向包括:
定義2次以上MainItems表和映射實體他們。
(能使用與此基本類型,與表每混凝土型在一起。)添加2分可爲空的FK列MainItems表指向自(到同一行),而不是AspectABOrBoth柱
( 1不空,如果MainItem是AspectA,其他非空,如果MainItem是AspectB。)
(能使用表拆分與此的基礎上,新的FK列。)
可以共享屬性,如我剛添加到示例中的「名稱」。 TPH繼承會很好,除非一行不能同時映射到兩個不同的派生類型。表條件分割是不可能的,至少不能通過設計者。 – 2011-04-27 20:05:10
這更多關於「讓我們試試看,你會看到」。這並非易事。並非所有東西都可以用EF映射。 – 2011-04-27 20:10:12
我認爲是這樣,將新模型映射到現有模式而不會破壞現有用法,這可能是一個挑戰。 – 2011-04-27 20:18:33