实体框架 - 将表拆分为具有重叠条件的多个实体
有没有办法执行以下映射(使用数据库优先方法):
表:(使用类似 C# 的语法定义表,仅出于可读性目的)< /i>
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。)向 MainItems 表中添加 2 个可空 FK 列,这些列指向 self(同一行)而不是 AspectABOrBoth 列
(如果 MainItem 是 AspectA,则 1 个非空,如果 MainItem 是 AspectB,则另一个非空。)
(可以基于新的 FK 列使用表拆分。)
Is there a way to preform the following mappings (using database-first approach):
Tables: (Defining tables with C#-like syntax for readability purposes only)
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;
}
Entities:
// 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; }
}
Possible directions I have considered but prefer not to take include:
Defining 2 views above MainItems table and mapping entities to them.
(Could use base type with this, together with Table-Per-Concrete-Type.)Adding 2 nullable FK columns to MainItems table that point to self (to same row) instead of AspectABOrBoth column
(1 not-null if MainItem is AspectA, the other not-null if MainItem is AspectB.)
(Could use table-splitting with this, based on new FK columns.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用以下命令时可以将表拆分为多个实体:
如果我看一下你的表,它看起来不像继承。
TableAItem
和TableBItem
没有任何共享属性。唯一的共享项可能是关键,这使您的设计的其余部分变得非常困难,因为与TableAMoreInfo
和TableBMoreInfo
的关系将使用MainItem
(持有者)创建键的)而不是子项目。视图看起来更适合解决这个问题,但默认情况下视图在 EF 中是只读的,除非您手动修改 SSDL。
Splitting table to multiple entities is possible when using:
If I look at your tables it doesn't look like inheritance.
TableAItem
andTableBItem
don't have any shared properties. The only shared item is probably key which makes rest of your design pretty hard because relation to bothTableAMoreInfo
andTableBMoreInfo
will be created withMainItem
(holder of the key) and not child items.Views look more suitable to solve this but view is by default read only in EF unless you manually modify SSDL.