实体框架 - 将表拆分为具有重叠条件的多个实体

发布于 2024-11-04 04:39:19 字数 2277 浏览 0 评论 0原文

有没有办法执行以下映射(使用数据库优先方法)

表:(使用类似 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; }
}

我考虑过但不喜欢采取的可能方向包括:

  1. 在 MainItems 表上方定义 2 个视图并将实体映射到它们。
    (可以使用基本类型,以及 Table-Per-Concrete-Type。)

  2. 向 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:

  1. Defining 2 views above MainItems table and mapping entities to them.
    (Could use base type with this, together with Table-Per-Concrete-Type.)

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-11-11 04:39:19

使用以下命令时可以将表拆分为多个实体:

  • 表拆分 - 它要求实体仅共享键,并且每个其他属性只能映射到单个实体。
  • TPH 继承 - 它要求基本实体定义关键和共享属性。子实体可以包含其他属性,但属性不能在子实体之间共享。表必须包含一个或多个特殊列(鉴别符),这些列将定义记录代表的继承层次结构中的类型。 MSL 不允许鉴别器使用任何复杂的表达式。复杂条件只能创建为所有条件的逻辑 AND。

如果我看一下你的表,它看起来不像继承。 TableAItemTableBItem 没有任何共享属性。唯一的共享项可能是关键,这使您的设计的其余部分变得非常困难,因为与 TableAMoreInfoTableBMoreInfo 的关系将使用 MainItem (持有者)创建键的)而不是子项目。

视图看起来更适合解决这个问题,但默认情况下视图在 EF 中是只读的,除非您手动修改 SSDL。

Splitting table to multiple entities is possible when using:

  • Table splitting - it requires that entity share only the key and each other property can be mapped only to single entity.
  • TPH inheritance - it requires that base entity defines key and shared properties. Sub entities can contain another properties but properties cannot be shared among sub entities. Table must contain one or more special columns (dicriminators) which will define what type in inheritance hierarchy the record represents. MSL doesn't allow any complex expression for discriminator. The complex condition can be only created as logical AND of all conditions.

If I look at your tables it doesn't look like inheritance. TableAItem and TableBItem don't have any shared properties. The only shared item is probably key which makes rest of your design pretty hard because relation to both TableAMoreInfo and TableBMoreInfo will be created with MainItem (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文