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