【问题标题】:Generate String Value Table Automatically in EF Core在 EF Core 中自动生成字符串值表
【发布时间】:2020-11-30 04:38:12
【问题描述】:

我有一个名为Customer 的表,其中有几个名为National CodeName 的列。它还具有许多其他功能,称为Contact NumbersRecommenders,因为Contact NumbersRecommenders 的数量不止一个,因此您需要一些其他表来存储它们。 还假设我有其他表,例如 Customer,每个表都有多个大于 1 的属性。

  1. 您对存储这些值有何建议?
  2. 在一个来源中,建议为每个表使用一个名为StringValue 的表来存储。 EF core 有没有办法实现StringValue 而无需编写额外的代码?

示例:

   Customer Table:

   CustomerId         Name         NationalCode
   ------------------------------------------------------------------------
    1                 David         xxxx
   ------------------------------------------------------------------------
  
   

   StringValue Table:

   StringId         CustomerId         StringName         Value
   ------------------------------------------------------------------------
   10                 1                PhoneNumber        915245
   11                 1                PhoneNumber        985452
   12                 1                PhoneNumber        935446
   13                 1                Recommenders       Mr Jhon
   14                 1                Recommenders       Mr bb
   ------------------------------------------------------------------------

【问题讨论】:

  • 在选择 EAV(实体属性值)路径之前,请先进行研究以了解其含义。它非常灵活 - 以相当复杂和低效率为代价。
  • 感谢您的提示。您认为什么是灵活高效的?

标签: database postgresql asp.net-core entity-framework-core ef-core-3.1


【解决方案1】:

我觉得更直观的做法是为有多个记录的字段创建一个新表,然后在两个表之间配置一对多的关系。以你的情况为例,你可以把客户表分成三张表,可以通过外键链接:

1.客户表:

CustomerId        Name         NationalCode
---------------------------------------------
1                 David        xxxx

2.联系表:

Id        CustomerId         PhoneNumber
---------------------------------------------
1         1                  915245
2         1                  985452
3         1                  935446

3.推荐人表:

Id        CustomerId         RecommenderName
---------------------------------------------
1         1                  Mr Jhon
2         1                  Mr bb

【讨论】:

  • 感谢您的回答。但我需要一些更好的解决方案。
猜你喜欢
  • 2019-10-04
  • 2022-01-17
  • 2019-02-06
  • 2015-11-04
  • 2013-07-12
  • 2022-01-20
  • 2019-11-01
  • 2021-04-18
  • 1970-01-01
相关资源
最近更新 更多