【问题标题】:Entity framework code first map entities to different tables实体框架代码首先将实体映射到不同的表
【发布时间】:2013-04-04 22:23:12
【问题描述】:

我有类似下面的课程。我想重用类并将它们保存在不同的表中,首先使用实体​​框架代码。

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsAvailable { get; set; }
    public List<Part> Parts { get; set; }
    public List<Promotion> Promotions { get; set; }
}

public class Field
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
}

public class Part
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Field> Details { get; set; }
}

public class Promotion
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Field> Details { get; set; }
}

我想映射我的实体,以便生成如下所示的数据库表。

产品:ID、名称、IsAvailable

ProductParts:ID、名称、ProductId

ProductPartDetails: ID、名称、值、ProductPartId

ProductPromotions:ID、名称、ProductId

ProductPromotionDetails:ID、名称、值、ProductPromotionId

我真正感兴趣的是我希望 Field 类被重用并存储在不同的表 ProductPartDetailsProductPromotionDetails 中,正如我所描述的更多。有可能还是我的方法需要改变?

【问题讨论】:

  • 你试过了吗?什么不起作用
  • 我想知道fluent api。目前,使用 ForeignKeys 创建并与 Product 和 Promotion 共享表“字段”。
  • 我听到了 :) 你可以 - 但你需要重新安排一切。您应该使多对手动“手动”(定义像 ProductPartDetails 等自定义类) - 为它定义流利的配置(例如 HasRequired(...).WithMany(...) 和 .HasKey(...) 和将Field 作为属性添加到该表中。创建字段ComplexType,以便“重用”(仅转换为字段,而不是单独的表)。两者都类似。如果你可以自己尝试一些东西关于这个 - 我现在没有时间 - 我会看看你做了什么,并在这些空白处感受。
  • 例如> here <,或者看看CategoryItemValue> here <
  • 您现在已经“15 岁以上”了 - 欢迎来到“选民”

标签: entity-framework dictionary code-first


【解决方案1】:

您可以 - 但您需要重新安排所有内容。

您应该制作多对手动的“手动”(定义自定义类,如 ProductPartDetails 等),

为它定义流利的配置

例如(更多伪代码)

[ComplexType()]
public class Field
{}
public class ProductPartDetails
{
    public int ProductId { get; set; }
    public int PartId { get; set; }
    public virtual Product Product { get; set; }
    public virtual Part Part { get; set; }
    public Field Field { get; set; }
}
modelBuilder.Entity<ProductPartDetails>()
    .HasKey(x => new { x.ProductId, x.PartId });

modelBuilder.Entity<ProductPartDetails>()
    .HasRequired(i => i.Product)
    .WithMany(u => u.Details)
    .HasForeignKey(i => i.ProductId)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<ProductPartDetails>()
    .HasRequired(i => i.Part)
    .WithMany(u => u.Details)
    .HasForeignKey(i => i.PartId)
    .WillCascadeOnDelete(false);

现在让Details 指向ProductPartDetails 而不是 Field.

...并将Field 作为属性添加到该表中。

制作字段 ComplexType 以便“重用”(仅转换为字段,而不是单独的表格)。两者都是这样的。

例如EF code-first many-to-many with additional data

Code First Fluent API and Navigation Properties in a Join Table
ASP.Net MVC 3 EF "Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths"

【讨论】:

  • 其实我一直在寻找流畅的 api 来将相同的 ComplexType 映射到不同的表中。但我最终创建了继承 Field 实体的自定义类,并按照您的建议将它们映射为一对多。感谢您的回复。
  • 复杂类型没有被映射 - 只是将它用作“复合”部分 - 你不需要ID - ID 将被制作(像它的任何属性一样)到 Field_ID、Field_Name、Field_Value等
猜你喜欢
  • 2011-08-29
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多