【问题标题】:EF6 Many to Many relationship, multiple tables to join tableEF6多对多关系,多表联表
【发布时间】:2018-07-31 20:18:35
【问题描述】:

抱歉,标题太长了,想不出更好的方法来总结这个问题。

我正在尝试使用 EF6 重写现有的 L2S 上下文,并且存在一个公认的问题关系,它遇到了问题。

EntityAttributes 充当许多表(包括材料和公式)与属性的连接表。

左侧的所有表都有一个 Id Identity 列和一个 EntityTypeId 列作为复合 PK。 EntityTypeId 列的值对每个表都是唯一的(Materials 为 1,Formulas 为 2,等等)

Attributes 表具有 Id (Identity) 和 EntityTypeId 的复合 PK。

EntityAttributes 表有一个 PK 的 Id(身份)和 AttributeId 和 EntityTypeId 作为对 Attributes 的 FK。

尝试创建 EntityAttribute 时,出现以下异常:

ReferentialConstraint 中的依赖属性映射到存储生成的列。列:'Id'。

我认为根本问题是与 EntityAttributes 的材料、公式等关系在数据库级别是无法执行的。 L2S 似乎并不介意这一点,但 EF6 似乎更符合底层数据存储的限制。

所以我的问题有两个: 1. 有没有办法让 EF 处理当前设置的这种关系? 2. 如果不是,有什么更好的设计来达到同样的效果?我可以完全控制数据库架构。

材质类:

public partial class Material
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EntityTypeId { get; set; }
}

EntityAttribute 类:

public partial class EntityAttribute
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Key]
    [Column(Order = 1)]
    public int AttributeId { get; set; }

    [Key]
    [Column(Order = 2)]
    public int EntityId { get; set; }

    [Key]
    [Column(Order = 3)]
    public int EntityTypeId { get; set; }
}

属性类:

public partial class Attribute
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EntityTypeId { get; set; }
}

OnModelCreating 中的关系定义:

    modelBuilder.Entity<Attribute>()
      .HasMany(e => e.EntityAttributes)
      .WithRequired(e => e.Attribute)
      .HasForeignKey(e => new { e.AttributeId, e.EntityTypeId });

我已经尝试了在 OnModelCreating 中更改属性和不同关系设置的所有组合,我已经在这方面旋转了 3 天,但再也看不到树木的木材了。

【问题讨论】:

  • 我认为你想要的是Table per Hierarchy,你有discriminator字段来确定你的实体是MaterialFormula
  • 你的意思是合并Materials and Formulas表并在合并表中使用discriminator?不幸的是,这些实体完全不同,因此不适合 TPH。
  • 不,这不是我的意思,这里有 2 个链接可能会帮助您解决问题 learnentityframeworkcore.com/inheritance/table-per-hierarchyweblogs.asp.net/manavi/…
  • 我明白了,在 EntityAttributes 表中有 TPH。会试一试的。
  • 这真是一种享受,真不敢相信我以前没想到!如果您将其作为答案,我会将其标记为已批准的答案。谢谢。

标签: c# entity-framework-6


【解决方案1】:

您想要的是Table per Hierarchy,其中EntityAttribute 的表将有discriminator 字段来确定您的实体是Material 还是Formula

public class EntityAttribute
{
    public int Id { get; set; }
    public int AttributeId { get; set; }
    public int EntityId { get; set; }
    ...
}

public class Material : EntityAttribute
{
    public int Id { get; set; }
    ...
}

public class Formula : EntityAttribute
{
    public int Id { get; set; }
    ...
}

以下链接可提供帮助:learn entity framework TPHinheritance mapping strategies with entity framework code first

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多